react-native-navigation的迁移库

RCCDrawerController.m 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #import "RCCDrawerController.h"
  2. #import "RCCViewController.h"
  3. #import "MMExampleDrawerVisualStateManager.h"
  4. #import "RCCDrawerHelper.h"
  5. #import <React/RCTConvert.h>
  6. #import "RCCManagerModule.h"
  7. #import "UIViewController+Rotation.h"
  8. #define RCCDRAWERCONTROLLER_ANIMATION_DURATION 0.33f
  9. @implementation RCCDrawerController
  10. @synthesize drawerStyle = _drawerStyle;
  11. UIViewController *leftViewController = nil;
  12. UIViewController *rightViewController = nil;
  13. -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
  14. return [self supportedControllerOrientations];
  15. }
  16. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge
  17. {
  18. self.drawerStyle = props[@"style"];
  19. // center
  20. if ([children count] < 1) return nil;
  21. UIViewController *centerViewController = [RCCViewController controllerWithLayout:children[0] globalProps:globalProps bridge:bridge];
  22. // left
  23. NSString *componentLeft = props[@"componentLeft"];
  24. NSDictionary *passPropsLeft = props[@"passPropsLeft"];
  25. NSDictionary *styleLeft = props[@"styleLeft"];
  26. if (componentLeft) leftViewController = [[RCCViewController alloc] initWithComponent:componentLeft passProps:passPropsLeft navigatorStyle:styleLeft globalProps:globalProps bridge:bridge];
  27. // right
  28. NSString *componentRight = props[@"componentRight"];
  29. NSDictionary *passPropsRight = props[@"passPropsRight"];
  30. NSDictionary *styleRight = props[@"styleRight"];
  31. if (componentRight) rightViewController = [[RCCViewController alloc] initWithComponent:componentRight passProps:passPropsRight navigatorStyle:styleRight globalProps:globalProps bridge:bridge];
  32. self = [super initWithCenterViewController:centerViewController
  33. leftDrawerViewController:leftViewController
  34. rightDrawerViewController:rightViewController];
  35. [self setAnimationTypeWithName:props[@"animationType"]];
  36. // default is all MMOpenDrawerGestureModeAll and MMCloseDrawerGestureModeAll
  37. self.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
  38. self.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
  39. NSNumber *disableOpenGesture = props[@"disableOpenGesture"];
  40. if ([disableOpenGesture boolValue]) {
  41. self.openDrawerGestureModeMask = MMOpenDrawerGestureModeNone;
  42. }
  43. [self setStyle];
  44. [self setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
  45. MMDrawerControllerDrawerVisualStateBlock block;
  46. block = [[MMExampleDrawerVisualStateManager sharedManager] drawerVisualStateBlockForDrawerSide:drawerSide];
  47. if (block) {
  48. block(drawerController, drawerSide, percentVisible);
  49. }
  50. }];
  51. [self setGestureStartBlock:^(MMDrawerController *drawerController, UIGestureRecognizer *gesture) {
  52. [RCCManagerModule cancelAllRCCViewControllerReactTouches];
  53. }];
  54. self.view.backgroundColor = [UIColor clearColor];
  55. [self setRotation:props];
  56. if (!self) return nil;
  57. return self;
  58. }
  59. -(void)setStyle {
  60. if (self.drawerStyle[@"drawerShadow"]) {
  61. self.showsShadow = ([self.drawerStyle[@"drawerShadow"] boolValue]) ? YES : NO;
  62. }
  63. NSNumber *leftDrawerWidth = self.drawerStyle[@"leftDrawerWidth"];
  64. if (leftDrawerWidth) {
  65. self.maximumLeftDrawerWidth = self.view.bounds.size.width * MIN(1, (leftDrawerWidth.floatValue/100.0));
  66. }
  67. NSNumber *rightDrawerWidth = self.drawerStyle[@"rightDrawerWidth"];
  68. if (rightDrawerWidth) {
  69. self.maximumRightDrawerWidth = self.view.bounds.size.width * MIN(1, (rightDrawerWidth.floatValue/100.0));
  70. }
  71. NSString *contentOverlayColor = self.drawerStyle[@"contentOverlayColor"];
  72. if (contentOverlayColor)
  73. {
  74. UIColor *color = contentOverlayColor != (id)[NSNull null] ? [RCTConvert UIColor:contentOverlayColor] : nil;
  75. [self setCenterOverlayColor:color];
  76. }
  77. if (self.drawerStyle[@"shouldStretchDrawer"]) {
  78. self.shouldStretchDrawer = ([self.drawerStyle[@"shouldStretchDrawer"] boolValue]) ? YES : NO;
  79. }
  80. }
  81. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge
  82. {
  83. MMDrawerSide side = MMDrawerSideLeft;
  84. if ([actionParams[@"side"] isEqualToString:@"right"]) side = MMDrawerSideRight;
  85. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  86. // open
  87. if ([performAction isEqualToString:@"open"])
  88. {
  89. [self openDrawerSide:side animated:animated completion:nil];
  90. return;
  91. }
  92. // close
  93. if ([performAction isEqualToString:@"close"])
  94. {
  95. if (self.openSide == side) {
  96. [self closeDrawerAnimated:animated completion:nil];
  97. }
  98. return;
  99. }
  100. // setDrawerEnabled
  101. if ([performAction isEqualToString:@"setDrawerEnabled"])
  102. {
  103. bool enabled = [actionParams[@"enabled"] boolValue];
  104. if ([actionParams[@"side"] isEqualToString:@"left"]) {
  105. [super setLeftDrawerViewController: enabled ? leftViewController : nil];
  106. } else if ([actionParams[@"side"] isEqualToString:@"right"]) {
  107. [super setRightDrawerViewController: enabled ? rightViewController : nil];
  108. }
  109. }
  110. // toggle
  111. if ([performAction isEqualToString:@"toggle"])
  112. {
  113. [super toggleDrawerSide:side animated:animated completion:nil];
  114. return;
  115. }
  116. // setStyle
  117. if ([performAction isEqualToString:@"setStyle"])
  118. {
  119. if (actionParams[@"animationType"]) {
  120. NSString *animationTypeString = actionParams[@"animationType"];
  121. [self setAnimationTypeWithName:animationTypeString];
  122. }
  123. return;
  124. }
  125. }
  126. -(void)setAnimationTypeWithName:(NSString*)animationTypeName {
  127. MMDrawerAnimationType animationType = MMDrawerAnimationTypeNone;
  128. if ([animationTypeName isEqualToString:@"door"]) animationType = MMDrawerAnimationTypeSwingingDoor;
  129. else if ([animationTypeName isEqualToString:@"parallax"]) animationType = MMDrawerAnimationTypeParallax;
  130. else if ([animationTypeName isEqualToString:@"slide"]) animationType = MMDrawerAnimationTypeSlide;
  131. else if ([animationTypeName isEqualToString:@"slide-and-scale"]) animationType = MMDrawerAnimationTypeSlideAndScale;
  132. [MMExampleDrawerVisualStateManager sharedManager].leftDrawerAnimationType = animationType;
  133. [MMExampleDrawerVisualStateManager sharedManager].rightDrawerAnimationType = animationType;
  134. }
  135. @end