react-native-navigation的迁移库

RNNSideMenuController.m 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #import "RNNSideMenuController.h"
  2. #import "RNNSideMenuChildVC.h"
  3. #import "MMDrawerController.h"
  4. #import "MMDrawerVisualState.h"
  5. @interface RNNSideMenuController ()
  6. @property (readwrite) RNNSideMenuChildVC *center;
  7. @property (readwrite) RNNSideMenuChildVC *left;
  8. @property (readwrite) RNNSideMenuChildVC *right;
  9. @end
  10. @implementation RNNSideMenuController
  11. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo childViewControllers:(NSArray *)childViewControllers options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions presenter:(RNNViewControllerPresenter *)presenter {
  12. [self setControllers:childViewControllers];
  13. self = [super initWithCenterViewController:self.center leftDrawerViewController:self.left rightDrawerViewController:self.right];
  14. self.presenter = presenter;
  15. [self.presenter bindViewController:self];
  16. self.defaultOptions = defaultOptions;
  17. self.options = options;
  18. self.layoutInfo = layoutInfo;
  19. self.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
  20. [self.presenter applyOptionsOnInit:self.resolveOptions];
  21. // Fixes #3697
  22. [self setExtendedLayoutIncludesOpaqueBars:YES];
  23. self.edgesForExtendedLayout |= UIRectEdgeBottom;
  24. return self;
  25. }
  26. - (void)willMoveToParentViewController:(UIViewController *)parent {
  27. if (parent) {
  28. [_presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  29. }
  30. }
  31. - (void)onChildWillAppear {
  32. [_presenter applyOptions:self.resolveOptions];
  33. [((UIViewController<RNNParentProtocol> *)self.parentViewController) onChildWillAppear];
  34. }
  35. - (RNNNavigationOptions *)resolveOptions {
  36. return [(RNNNavigationOptions *)[self.options mergeInOptions:self.getCurrentChild.resolveOptions.copy] withDefault:self.defaultOptions];
  37. }
  38. - (void)mergeOptions:(RNNNavigationOptions *)options {
  39. [_presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
  40. [((UIViewController<RNNLayoutProtocol> *)self.parentViewController) mergeOptions:options];
  41. }
  42. - (void)overrideOptions:(RNNNavigationOptions *)options {
  43. [self.options overrideOptions:options];
  44. }
  45. - (void)renderTreeAndWait:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  46. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  47. dispatch_group_t group = dispatch_group_create();
  48. for (UIViewController<RNNLayoutProtocol>* childViewController in self.childViewControllers) {
  49. dispatch_group_enter(group);
  50. dispatch_async(dispatch_get_main_queue(), ^{
  51. [childViewController renderTreeAndWait:wait perform:^{
  52. dispatch_group_leave(group);
  53. }];
  54. });
  55. }
  56. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  57. dispatch_async(dispatch_get_main_queue(), ^{
  58. readyBlock();
  59. });
  60. });
  61. }
  62. - (void)setAnimationType:(NSString *)animationType {
  63. MMDrawerControllerDrawerVisualStateBlock animationTypeStateBlock = nil;
  64. if ([animationType isEqualToString:@"door"]) animationTypeStateBlock = [MMDrawerVisualState swingingDoorVisualStateBlock];
  65. else if ([animationType isEqualToString:@"parallax"]) animationTypeStateBlock = [MMDrawerVisualState parallaxVisualStateBlockWithParallaxFactor:2.0];
  66. else if ([animationType isEqualToString:@"slide"]) animationTypeStateBlock = [MMDrawerVisualState slideVisualStateBlock];
  67. else if ([animationType isEqualToString:@"slide-and-scale"]) animationTypeStateBlock = [MMDrawerVisualState slideAndScaleVisualStateBlock];
  68. if (animationTypeStateBlock) {
  69. [self setDrawerVisualStateBlock:animationTypeStateBlock];
  70. }
  71. }
  72. - (void)side:(MMDrawerSide)side width:(double)width {
  73. switch (side) {
  74. case MMDrawerSideRight:
  75. self.maximumRightDrawerWidth = width;
  76. [self.right setWidth:width];
  77. break;
  78. case MMDrawerSideLeft:
  79. self.maximumLeftDrawerWidth = width;
  80. [self.left setWidth:width];
  81. default:
  82. break;
  83. }
  84. }
  85. - (void)side:(MMDrawerSide)side visible:(BOOL)visible {
  86. if (visible) {
  87. [self showSideMenu:side animated:YES];
  88. } else {
  89. [self hideSideMenu:side animated:YES];
  90. }
  91. }
  92. -(void)showSideMenu:(MMDrawerSide)side animated:(BOOL)animated {
  93. [self openDrawerSide:side animated:animated completion:nil];
  94. }
  95. -(void)hideSideMenu:(MMDrawerSide)side animated:(BOOL)animated {
  96. [self closeDrawerAnimated:animated completion:nil];
  97. }
  98. - (void)side:(MMDrawerSide)side enabled:(BOOL)enabled {
  99. switch (side) {
  100. case MMDrawerSideRight:
  101. self.rightSideEnabled = enabled;
  102. break;
  103. case MMDrawerSideLeft:
  104. self.leftSideEnabled = enabled;
  105. default:
  106. break;
  107. }
  108. }
  109. -(void)setControllers:(NSArray*)controllers {
  110. for (id controller in controllers) {
  111. if ([controller isKindOfClass:[RNNSideMenuChildVC class]]) {
  112. RNNSideMenuChildVC *child = (RNNSideMenuChildVC*)controller;
  113. if (child.type == RNNSideMenuChildTypeCenter) {
  114. self.center = child;
  115. }
  116. else if(child.type == RNNSideMenuChildTypeLeft) {
  117. self.left = child;
  118. }
  119. else if(child.type == RNNSideMenuChildTypeRight) {
  120. self.right = child;
  121. }
  122. [self addChildViewController:child];
  123. }
  124. else {
  125. @throw [NSException exceptionWithName:@"UnknownSideMenuControllerType" reason:[@"Unknown side menu type " stringByAppendingString:[controller description]] userInfo:nil];
  126. }
  127. }
  128. }
  129. - (UIStatusBarStyle)preferredStatusBarStyle {
  130. return self.openedViewController.preferredStatusBarStyle;
  131. }
  132. - (UIViewController *)openedViewController {
  133. switch (self.openSide) {
  134. case MMDrawerSideNone:
  135. return self.center;
  136. case MMDrawerSideLeft:
  137. return self.left;
  138. case MMDrawerSideRight:
  139. return self.right;
  140. default:
  141. return self.center;
  142. break;
  143. }
  144. }
  145. - (UIViewController<RNNLayoutProtocol> *)getCurrentChild {
  146. return self.center;
  147. }
  148. @end