react-native-navigation的迁移库

RCCTheSideBarManagerViewController.m 8.5KB

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