react-native-navigation的迁移库

RCCTheSideBarManagerViewController.m 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // DarwerTheSideBarManagerViewController.m
  3. // ReactNativeControllers
  4. //
  5. // Created by Ran Greenberg on 22/03/2016.
  6. // Copyright © 2016 artal. All rights reserved.
  7. //
  8. #import "RCCTheSideBarManagerViewController.h"
  9. #import "RCCViewController.h"
  10. #import "RCCDrawerHelper.h"
  11. #import <React/RCTConvert.h>
  12. #import "UIViewController+Rotation.h"
  13. @interface RCCTheSideBarManagerViewController () <TheSidebarControllerDelegate>
  14. @property (nonatomic) BOOL isOpen;
  15. @property (nonatomic) SidebarTransitionStyle animationStyle;
  16. @property (nonatomic, strong) RCCViewController *leftViewController;
  17. @property (nonatomic, strong) RCCViewController *rightViewController;
  18. @property (nonatomic, strong) RCCViewController *centerViewController;
  19. @property (nonatomic, strong) UIColor *originalWindowBackgroundColor;
  20. @end
  21. @implementation RCCTheSideBarManagerViewController
  22. @synthesize overlayButton = _overlayButton, drawerStyle = _drawerStyle;
  23. -(UIButton*)overlayButton {
  24. if (!_overlayButton) {
  25. _overlayButton = [RCCDrawerHelper createOverlayButton:self];
  26. }
  27. return _overlayButton;
  28. }
  29. -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
  30. return [self supportedControllerOrientations];
  31. }
  32. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge {
  33. if ([children count] < 1) return nil;
  34. UIViewController *centerVC = [RCCViewController controllerWithLayout:children[0] globalProps:props bridge:bridge];
  35. UIViewController *leftVC = nil;
  36. UIViewController *rightVC = nil;
  37. // left
  38. NSString *componentLeft = props[@"componentLeft"];
  39. if (componentLeft) {
  40. leftVC = [[RCCViewController alloc] initWithComponent:componentLeft passProps:props[@"passPropsLeft"] navigatorStyle:nil globalProps:props bridge:bridge];
  41. }
  42. // right
  43. NSString *componentRight = props[@"componentRight"];
  44. if (componentRight) {
  45. rightVC = [[RCCViewController alloc] initWithComponent:componentRight passProps:props[@"passPropsRight"] navigatorStyle:nil globalProps:props bridge:bridge];
  46. }
  47. self = [super initWithContentViewController:centerVC
  48. leftSidebarViewController:leftVC
  49. rightSidebarViewController:rightVC];
  50. if (!self) return nil;
  51. self.leftViewController = leftVC;
  52. self.rightViewController = rightVC;
  53. self.centerViewController = centerVC;
  54. self.drawerStyle = props[@"style"];
  55. self.delegate = self;
  56. self.isOpen = NO;
  57. [self setAnimationType:props[@"animationType"]];
  58. [self setStyle];
  59. [self setRotation:props];
  60. return self;
  61. }
  62. -(void)viewDidDisappear:(BOOL)animated {
  63. [super viewDidDisappear:animated];
  64. UIWindow *appDelegateWindow = [[[UIApplication sharedApplication] delegate] window];
  65. [appDelegateWindow setBackgroundColor:self.originalWindowBackgroundColor];
  66. }
  67. -(void)setStyle:(TheSideBarSide)side {
  68. if(side == TheSideBarSideLeft && !self.leftViewController) return;
  69. if(side == TheSideBarSideRight && !self.rightViewController) return;
  70. CGRect sideBarFrame = self.view.frame;
  71. switch (side) {
  72. case TheSideBarSideLeft:
  73. {
  74. NSNumber *leftDrawerWidth = self.drawerStyle[@"leftDrawerWidth"];
  75. if (!leftDrawerWidth) leftDrawerWidth = [NSNumber numberWithInteger:DRAWER_DEFAULT_WIDTH_PERCENTAGE];
  76. self.visibleWidth = self.view.bounds.size.width * MIN(1, (leftDrawerWidth.floatValue/100.0));
  77. sideBarFrame.size.width = self.view.bounds.size.width * MIN(1, (leftDrawerWidth.floatValue/100.0));
  78. self.leftViewController.view.frame = sideBarFrame;
  79. }
  80. break;
  81. case TheSideBarSideRight:
  82. {
  83. NSNumber *rightDrawerWidth = self.drawerStyle[@"rightDrawerWidth"];
  84. if (!rightDrawerWidth) rightDrawerWidth = [NSNumber numberWithInteger:DRAWER_DEFAULT_WIDTH_PERCENTAGE];
  85. self.visibleWidth = self.view.bounds.size.width * MIN(1, (rightDrawerWidth.floatValue/100.0));
  86. sideBarFrame.size.width = self.view.bounds.size.width * MIN(1, (rightDrawerWidth.floatValue/100.0));
  87. sideBarFrame.origin.x = self.view.frame.size.width - self.visibleWidth;
  88. self.rightViewController.view.frame = sideBarFrame;
  89. }
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. -(void)setStyle {
  96. [self setStyle:TheSideBarSideLeft];
  97. [self setStyle:TheSideBarSideRight];
  98. NSString *contentOverlayColor = self.drawerStyle[@"contentOverlayColor"];
  99. if (contentOverlayColor)
  100. {
  101. UIColor *color = contentOverlayColor != (id)[NSNull null] ? [RCTConvert UIColor:contentOverlayColor] : nil;
  102. self.overlayContentColor = color;
  103. }
  104. UIImage *backgroundImage = nil;
  105. id icon = self.drawerStyle[@"backgroundImage"];
  106. UIWindow *appDelegateWindow = [[[UIApplication sharedApplication] delegate] window];
  107. self.originalWindowBackgroundColor = appDelegateWindow.backgroundColor;
  108. if (icon)
  109. {
  110. backgroundImage = [RCTConvert UIImage:icon];
  111. if (backgroundImage) {
  112. backgroundImage = [RCCDrawerHelper imageWithImage:backgroundImage scaledToSize:appDelegateWindow.bounds.size];
  113. [appDelegateWindow setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];
  114. }
  115. }
  116. }
  117. -(void)setAnimationType:(NSString*)type {
  118. if ([type isEqualToString:@"airbnb"]) self.animationStyle = SidebarTransitionStyleAirbnb;
  119. else if ([type isEqualToString:@"facebook"]) self.animationStyle = SidebarTransitionStyleFacebook;
  120. else if ([type isEqualToString:@"luvocracy"]) self.animationStyle = SidebarTransitionStyleLuvocracy;
  121. else if ([type isEqualToString:@"wunder-list"]) self.animationStyle = SidebarTransitionStyleWunderlist;
  122. // currently unsuported animation types
  123. // else if ([type isEqualToString:@"feedly"]) self.animationStyle = SidebarTransitionStyleFeedly;
  124. // else if ([type isEqualToString:@"flipboard"]) self.animationStyle = SidebarTransitionStyleFlipboard;
  125. // default
  126. else self.animationStyle = SidebarTransitionStyleAirbnb;
  127. }
  128. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge {
  129. TheSideBarSide side = TheSideBarSideLeft;
  130. if ([actionParams[@"side"] isEqualToString:@"right"]) side = TheSideBarSideRight;
  131. // open
  132. if ([performAction isEqualToString:@"open"])
  133. {
  134. [self openSideMenu:side];
  135. return;
  136. }
  137. // close
  138. if ([performAction isEqualToString:@"close"])
  139. {
  140. [self overlayButtonPressed:self.overlayButton];
  141. return;
  142. }
  143. // toggle
  144. if ([performAction isEqualToString:@"toggle"])
  145. {
  146. [self setStyle:side];
  147. if (self.isOpen) {
  148. [self overlayButtonPressed:self.overlayButton];
  149. }
  150. else {
  151. [self openSideMenu:side];
  152. }
  153. self.isOpen = !self.isOpen;
  154. return;
  155. }
  156. // setStyle
  157. if ([performAction isEqualToString:@"setStyle"])
  158. {
  159. if (actionParams[@"animationType"]) {
  160. NSString *animationTypeString = actionParams[@"animationType"];
  161. CGRect leftSideBarFrame = self.leftViewController.view.frame;
  162. leftSideBarFrame.origin.x = 0;
  163. self.leftViewController.view.frame = leftSideBarFrame;
  164. CGRect rightSideBarFrame = self.rightViewController.view.frame;
  165. rightSideBarFrame.origin.x = self.view.frame.size.width - self.visibleWidth;
  166. self.rightViewController.view.frame = rightSideBarFrame;
  167. [self setAnimationType:animationTypeString];
  168. }
  169. return;
  170. }
  171. }
  172. -(void)openSideMenu:(TheSideBarSide)side{
  173. RCCDrawerSide drawerSide;
  174. switch (side) {
  175. case TheSideBarSideLeft:
  176. {
  177. [self presentLeftSidebarViewControllerWithStyle:self.animationStyle];
  178. drawerSide = RCCDrawerSideLeft;
  179. }
  180. break;
  181. case TheSideBarSideRight:
  182. {
  183. [self presentRightSidebarViewControllerWithStyle:self.animationStyle];
  184. drawerSide = RCCDrawerSideRight;
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. [RCCDrawerHelper addOverlayButtonToScreen:self.overlayButton contextView:self.view side:drawerSide sideMenuWidth:self.visibleWidth animationDuration:self.animationDuration];
  191. }
  192. -(void)overlayButtonPressed:(UIButton*)button {
  193. [self dismissSidebarViewController];
  194. [RCCDrawerHelper overlayButtonPressed:button animationDuration:self.animationDuration];
  195. self.isOpen = NO;
  196. }
  197. @end