react-native-navigation的迁移库

RCCDrawerController.m 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  76. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge
  77. {
  78. MMDrawerSide side = MMDrawerSideLeft;
  79. if ([actionParams[@"side"] isEqualToString:@"right"]) side = MMDrawerSideRight;
  80. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  81. // open
  82. if ([performAction isEqualToString:@"open"])
  83. {
  84. [self openDrawerSide:side animated:animated completion:nil];
  85. return;
  86. }
  87. // close
  88. if ([performAction isEqualToString:@"close"])
  89. {
  90. if (self.openSide == side) {
  91. [self closeDrawerAnimated:animated completion:nil];
  92. }
  93. return;
  94. }
  95. // setDrawerEnabled
  96. if ([performAction isEqualToString:@"setDrawerEnabled"])
  97. {
  98. bool enabled = [actionParams[@"enabled"] boolValue];
  99. if ([actionParams[@"side"] isEqualToString:@"left"]) {
  100. [super setLeftDrawerViewController: enabled ? leftViewController : nil];
  101. } else if ([actionParams[@"side"] isEqualToString:@"right"]) {
  102. [super setRightDrawerViewController: enabled ? rightViewController : nil];
  103. }
  104. }
  105. // toggle
  106. if ([performAction isEqualToString:@"toggle"])
  107. {
  108. [super toggleDrawerSide:side animated:animated completion:nil];
  109. return;
  110. }
  111. // setStyle
  112. if ([performAction isEqualToString:@"setStyle"])
  113. {
  114. if (actionParams[@"animationType"]) {
  115. NSString *animationTypeString = actionParams[@"animationType"];
  116. [self setAnimationTypeWithName:animationTypeString];
  117. }
  118. return;
  119. }
  120. }
  121. -(void)setAnimationTypeWithName:(NSString*)animationTypeName {
  122. MMDrawerAnimationType animationType = MMDrawerAnimationTypeNone;
  123. if ([animationTypeName isEqualToString:@"door"]) animationType = MMDrawerAnimationTypeSwingingDoor;
  124. else if ([animationTypeName isEqualToString:@"parallax"]) animationType = MMDrawerAnimationTypeParallax;
  125. else if ([animationTypeName isEqualToString:@"slide"]) animationType = MMDrawerAnimationTypeSlide;
  126. else if ([animationTypeName isEqualToString:@"slide-and-scale"]) animationType = MMDrawerAnimationTypeSlideAndScale;
  127. [MMExampleDrawerVisualStateManager sharedManager].leftDrawerAnimationType = animationType;
  128. [MMExampleDrawerVisualStateManager sharedManager].rightDrawerAnimationType = animationType;
  129. }
  130. @end