react-native-navigation的迁移库

RCCLightBox.m 8.1KB

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