react-native-navigation的迁移库

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