react-native-navigation的迁移库

RCCLightBox.m 8.3KB

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