react-native-navigation的迁移库

RCCLightBox.m 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  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. if(!self.yellowBoxRemoved)
  75. {
  76. self.yellowBoxRemoved = [RCTHelpers removeYellowBox:self.reactView];
  77. }
  78. }
  79. -(void)removeAllObservers
  80. {
  81. [[NSNotificationCenter defaultCenter] removeObserver:self];
  82. [self.reactView.contentView.layer removeObserver:self forKeyPath:@"frame" context:nil];
  83. [self.reactView.contentView.layer removeObserver:self forKeyPath:@"bounds" context:NULL];
  84. }
  85. -(void)dealloc
  86. {
  87. [self removeAllObservers];
  88. }
  89. -(void)onRNReload
  90. {
  91. [self removeAllObservers];
  92. [self removeFromSuperview];
  93. self.reactView = nil;
  94. }
  95. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  96. {
  97. CGSize frameSize = CGSizeZero;
  98. if ([object isKindOfClass:[CALayer class]])
  99. frameSize = ((CALayer*)object).frame.size;
  100. if ([object isKindOfClass:[UIView class]])
  101. frameSize = ((UIView*)object).frame.size;
  102. if (!CGSizeEqualToSize(frameSize, self.reactView.frame.size))
  103. {
  104. self.reactView.frame = CGRectMake((self.frame.size.width - frameSize.width) * 0.5, (self.frame.size.height - frameSize.height) * 0.5, frameSize.width, frameSize.height);
  105. }
  106. }
  107. -(UIBlurEffect*)blurEfectForCurrentStyle
  108. {
  109. NSDictionary *style = self.params[@"style"];
  110. NSString *backgroundBlur = style[@"backgroundBlur"];
  111. if ([backgroundBlur isEqualToString:@"none"])
  112. {
  113. return nil;
  114. }
  115. UIBlurEffectStyle blurEffectStyle = UIBlurEffectStyleDark;
  116. if ([backgroundBlur isEqualToString:@"light"])
  117. blurEffectStyle = UIBlurEffectStyleLight;
  118. else if ([backgroundBlur isEqualToString:@"xlight"])
  119. blurEffectStyle = UIBlurEffectStyleExtraLight;
  120. else if ([backgroundBlur isEqualToString:@"dark"])
  121. blurEffectStyle = UIBlurEffectStyleDark;
  122. return [UIBlurEffect effectWithStyle:blurEffectStyle];
  123. }
  124. -(void)showAnimated
  125. {
  126. if (self.visualEffectView != nil || self.overlayColorView != nil)
  127. {
  128. [UIView animateWithDuration:0.3 animations:^()
  129. {
  130. if (self.visualEffectView != nil)
  131. {
  132. self.visualEffectView.effect = [self blurEfectForCurrentStyle];
  133. }
  134. if (self.overlayColorView != nil)
  135. {
  136. self.overlayColorView.alpha = 1;
  137. }
  138. }];
  139. }
  140. self.reactView.transform = CGAffineTransformMakeTranslation(0, 100);
  141. self.reactView.alpha = 0;
  142. [UIView animateWithDuration:0.6 delay:0.2 usingSpringWithDamping:0.65 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^()
  143. {
  144. self.reactView.transform = CGAffineTransformIdentity;
  145. self.reactView.alpha = 1;
  146. } completion:nil];
  147. }
  148. -(void)dismissAnimated
  149. {
  150. BOOL hasOverlayViews = (self.visualEffectView != nil || self.overlayColorView != nil);
  151. [UIView animateWithDuration:0.2 animations:^()
  152. {
  153. self.reactView.transform = CGAffineTransformMakeTranslation(0, 80);
  154. self.reactView.alpha = 0;
  155. }
  156. completion:^(BOOL finished)
  157. {
  158. if (!hasOverlayViews)
  159. {
  160. [self removeFromSuperview];
  161. }
  162. }];
  163. if (hasOverlayViews)
  164. {
  165. [UIView animateWithDuration:0.25 delay:0.15 options:UIViewAnimationOptionCurveEaseOut animations:^()
  166. {
  167. if (self.visualEffectView != nil)
  168. {
  169. self.visualEffectView.effect = nil;
  170. }
  171. if (self.overlayColorView != nil)
  172. {
  173. self.overlayColorView.alpha = 0;
  174. }
  175. } completion:^(BOOL finished)
  176. {
  177. [self removeFromSuperview];
  178. }];
  179. }
  180. }
  181. @end
  182. @implementation RCCLightBox
  183. +(void)showWithParams:(NSDictionary*)params
  184. {
  185. UIViewController *viewController = RCTPresentedViewController();
  186. if ([viewController.view viewWithTag:kLightBoxTag] != nil)
  187. {
  188. return;
  189. }
  190. RCCLightBoxView *lightBox = [[RCCLightBoxView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) params:params];
  191. lightBox.tag = kLightBoxTag;
  192. [viewController.view addSubview:lightBox];
  193. [lightBox showAnimated];
  194. }
  195. +(void)dismiss
  196. {
  197. UIViewController *viewController = RCTPresentedViewController();
  198. RCCLightBoxView *lightBox = [viewController.view viewWithTag:kLightBoxTag];
  199. if (lightBox != nil)
  200. {
  201. [lightBox dismissAnimated];
  202. }
  203. }
  204. @end