react-native-navigation的迁移库

RCCDrawerController.m 6.4KB

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