react-native-navigation的迁移库

RCCLightBox.m 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #import "RCCLightBox.h"
  2. #import "RCCManager.h"
  3. #import "RCTRootView.h"
  4. #import "RCTRootViewDelegate.h"
  5. #import "RCTConvert.h"
  6. #import "RCTHelpers.h"
  7. #import <objc/runtime.h>
  8. const NSInteger kLightBoxTag = 0x101010;
  9. @interface RCCLightBoxView : UIView
  10. @property (nonatomic, strong) RCTRootView *reactView;
  11. @property (nonatomic, strong) UIVisualEffectView *visualEffectView;
  12. @property (nonatomic, strong) UIView *overlayColorView;
  13. @property (nonatomic, strong) NSDictionary *params;
  14. @property (nonatomic) BOOL yellowBoxRemoved;
  15. @end
  16. @implementation RCCLightBoxView
  17. -(instancetype)initWithFrame:(CGRect)frame params:(NSDictionary*)params
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self)
  21. {
  22. self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  23. self.params = params;
  24. self.yellowBoxRemoved = NO;
  25. NSDictionary *passProps = self.params[@"passProps"];
  26. NSDictionary *style = self.params[@"style"];
  27. if (self.params != nil && style != nil)
  28. {
  29. if (style[@"backgroundBlur"] != nil && ![style[@"backgroundBlur"] isEqualToString:@"none"])
  30. {
  31. self.visualEffectView = [[UIVisualEffectView alloc] init];
  32. self.visualEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  33. self.visualEffectView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
  34. [self addSubview:self.visualEffectView];
  35. }
  36. if (style[@"backgroundColor"] != nil)
  37. {
  38. UIColor *backgroundColor = [RCTConvert UIColor:style[@"backgroundColor"]];
  39. if (backgroundColor != nil)
  40. {
  41. self.overlayColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  42. self.overlayColorView.backgroundColor = backgroundColor;
  43. self.overlayColorView.alpha = 0;
  44. [self addSubview:self.overlayColorView];
  45. }
  46. }
  47. }
  48. self.reactView = [[RCTRootView alloc] initWithBridge:[[RCCManager sharedInstance] getBridge] moduleName:self.params[@"component"] initialProperties:passProps];
  49. self.reactView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  50. self.reactView.backgroundColor = [UIColor clearColor];
  51. self.reactView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
  52. self.reactView.center = self.center;
  53. [self addSubview:self.reactView];
  54. [self.reactView.contentView.layer addObserver:self forKeyPath:@"frame" options:0 context:nil];
  55. [self.reactView.contentView.layer addObserver:self forKeyPath:@"bounds" options:0 context:NULL];
  56. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTReloadNotification object:nil];
  57. }
  58. return self;
  59. }
  60. -(void)layoutSubviews
  61. {
  62. [super layoutSubviews];
  63. if(!self.yellowBoxRemoved)
  64. {
  65. self.yellowBoxRemoved = [RCTHelpers removeYellowBox:self.reactView];
  66. }
  67. }
  68. -(void)removeAllObservers
  69. {
  70. [[NSNotificationCenter defaultCenter] removeObserver:self];
  71. [self.reactView.contentView.layer removeObserver:self forKeyPath:@"frame" context:nil];
  72. [self.reactView.contentView.layer removeObserver:self forKeyPath:@"bounds" context:NULL];
  73. }
  74. -(void)dealloc
  75. {
  76. [self removeAllObservers];
  77. }
  78. -(void)onRNReload
  79. {
  80. [self removeAllObservers];
  81. [self removeFromSuperview];
  82. self.reactView = nil;
  83. }
  84. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  85. {
  86. CGSize frameSize = CGSizeZero;
  87. if ([object isKindOfClass:[CALayer class]])
  88. frameSize = ((CALayer*)object).frame.size;
  89. if ([object isKindOfClass:[UIView class]])
  90. frameSize = ((UIView*)object).frame.size;
  91. if (!CGSizeEqualToSize(frameSize, self.reactView.frame.size))
  92. {
  93. self.reactView.frame = CGRectMake((self.frame.size.width - frameSize.width) * 0.5, (self.frame.size.height - frameSize.height) * 0.5, frameSize.width, frameSize.height);
  94. }
  95. }
  96. -(UIBlurEffect*)blurEfectForCurrentStyle
  97. {
  98. NSDictionary *style = self.params[@"style"];
  99. NSString *backgroundBlur = style[@"backgroundBlur"];
  100. if ([backgroundBlur isEqualToString:@"none"])
  101. {
  102. return nil;
  103. }
  104. UIBlurEffectStyle blurEffectStyle = UIBlurEffectStyleDark;
  105. if ([backgroundBlur isEqualToString:@"light"])
  106. blurEffectStyle = UIBlurEffectStyleLight;
  107. else if ([backgroundBlur isEqualToString:@"xlight"])
  108. blurEffectStyle = UIBlurEffectStyleExtraLight;
  109. else if ([backgroundBlur isEqualToString:@"dark"])
  110. blurEffectStyle = UIBlurEffectStyleDark;
  111. return [UIBlurEffect effectWithStyle:blurEffectStyle];
  112. }
  113. -(void)showAnimated
  114. {
  115. if (self.visualEffectView != nil || self.overlayColorView != nil)
  116. {
  117. [UIView animateWithDuration:0.3 animations:^()
  118. {
  119. if (self.visualEffectView != nil)
  120. {
  121. self.visualEffectView.effect = [self blurEfectForCurrentStyle];
  122. }
  123. if (self.overlayColorView != nil)
  124. {
  125. self.overlayColorView.alpha = 1;
  126. }
  127. }];
  128. }
  129. self.reactView.transform = CGAffineTransformMakeTranslation(0, 100);
  130. self.reactView.alpha = 0;
  131. [UIView animateWithDuration:0.6 delay:0.2 usingSpringWithDamping:0.65 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^()
  132. {
  133. self.reactView.transform = CGAffineTransformIdentity;
  134. self.reactView.alpha = 1;
  135. } completion:nil];
  136. }
  137. -(void)dismissAnimated
  138. {
  139. BOOL hasOverlayViews = (self.visualEffectView != nil || self.overlayColorView != nil);
  140. [UIView animateWithDuration:0.2 animations:^()
  141. {
  142. self.reactView.transform = CGAffineTransformMakeTranslation(0, 80);
  143. self.reactView.alpha = 0;
  144. }
  145. completion:^(BOOL finished)
  146. {
  147. if (!hasOverlayViews)
  148. {
  149. [self removeFromSuperview];
  150. }
  151. }];
  152. if (hasOverlayViews)
  153. {
  154. [UIView animateWithDuration:0.25 delay:0.15 options:UIViewAnimationOptionCurveEaseOut animations:^()
  155. {
  156. if (self.visualEffectView != nil)
  157. {
  158. self.visualEffectView.effect = nil;
  159. }
  160. if (self.overlayColorView != nil)
  161. {
  162. self.overlayColorView.alpha = 0;
  163. }
  164. } completion:^(BOOL finished)
  165. {
  166. [self removeFromSuperview];
  167. }];
  168. }
  169. }
  170. @end
  171. @implementation RCCLightBox
  172. +(UIWindow*)getWindow
  173. {
  174. UIApplication *app = [UIApplication sharedApplication];
  175. UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0];
  176. return window;
  177. }
  178. +(void)showWithParams:(NSDictionary*)params
  179. {
  180. UIWindow *window = [RCCLightBox getWindow];
  181. if ([window viewWithTag:kLightBoxTag] != nil)
  182. {
  183. return;
  184. }
  185. RCCLightBoxView *lightBox = [[RCCLightBoxView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) params:params];
  186. lightBox.tag = kLightBoxTag;
  187. [window addSubview:lightBox];
  188. [lightBox showAnimated];
  189. }
  190. +(void)dismiss
  191. {
  192. UIWindow *window = [RCCLightBox getWindow];
  193. RCCLightBoxView *lightBox = [window viewWithTag:kLightBoxTag];
  194. if (lightBox != nil)
  195. {
  196. [lightBox dismissAnimated];
  197. }
  198. }
  199. @end