react-native-navigation的迁移库

RCCDrawerController.m 5.6KB

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