react-native-navigation的迁移库

RNNBottomTabsController.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #import "RNNBottomTabsController.h"
  2. #import "UITabBarController+RNNUtils.h"
  3. @interface RNNBottomTabsController ()
  4. @property (nonatomic, strong) BottomTabPresenter* bottomTabPresenter;
  5. @property (nonatomic, strong) RNNDotIndicatorPresenter* dotIndicatorPresenter;
  6. @property (nonatomic) BOOL viewWillAppearOnce;
  7. @end
  8. @implementation RNNBottomTabsController {
  9. NSUInteger _currentTabIndex;
  10. BottomTabsBaseAttacher* _bottomTabsAttacher;
  11. }
  12. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
  13. creator:(id<RNNComponentViewCreator>)creator
  14. options:(RNNNavigationOptions *)options
  15. defaultOptions:(RNNNavigationOptions *)defaultOptions
  16. presenter:(RNNBasePresenter *)presenter
  17. bottomTabPresenter:(BottomTabPresenter *)bottomTabPresenter
  18. dotIndicatorPresenter:(RNNDotIndicatorPresenter *)dotIndicatorPresenter
  19. eventEmitter:(RNNEventEmitter *)eventEmitter
  20. childViewControllers:(NSArray *)childViewControllers
  21. bottomTabsAttacher:(BottomTabsBaseAttacher *)bottomTabsAttacher {
  22. _bottomTabsAttacher = bottomTabsAttacher;
  23. _bottomTabPresenter = bottomTabPresenter;
  24. _dotIndicatorPresenter = dotIndicatorPresenter;
  25. _pendingChildViewControllers = childViewControllers;
  26. self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter childViewControllers:childViewControllers];
  27. if (@available(iOS 13.0, *)) {
  28. self.tabBar.standardAppearance = [UITabBarAppearance new];
  29. }
  30. return self;
  31. }
  32. - (void)viewWillAppear:(BOOL)animated {
  33. [super viewWillAppear:animated];
  34. _viewWillAppearOnce = YES;
  35. [self loadChildren:self.pendingChildViewControllers];
  36. }
  37. - (void)onChildAddToParent:(UIViewController *)child options:(RNNNavigationOptions *)options {
  38. [_bottomTabPresenter applyOptionsOnWillMoveToParentViewController:options child:child];
  39. }
  40. - (void)mergeChildOptions:(RNNNavigationOptions *)options child:(UIViewController *)child {
  41. [super mergeChildOptions:options child:child];
  42. UIViewController* childViewController = [self findViewController:child];
  43. [_bottomTabPresenter mergeOptions:options resolvedOptions:childViewController.resolveOptions child:childViewController];
  44. [_dotIndicatorPresenter mergeOptions:options resolvedOptions:childViewController.resolveOptions child:childViewController];
  45. }
  46. - (id<UITabBarControllerDelegate>)delegate {
  47. return self;
  48. }
  49. - (void)render {
  50. [_bottomTabsAttacher attach:self];
  51. }
  52. - (void)viewDidLayoutSubviews {
  53. [self.presenter viewDidLayoutSubviews];
  54. for (UIView *view in [[self tabBar] subviews]) {
  55. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleLongPress:)];
  56. if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) {
  57. [view addGestureRecognizer: longPressGesture];
  58. }
  59. }
  60. [_dotIndicatorPresenter bottomTabsDidLayoutSubviews:self];
  61. }
  62. - (UIViewController *)getCurrentChild {
  63. return self.selectedViewController;
  64. }
  65. - (CGFloat)getBottomTabsHeight {
  66. return self.tabBar.frame.size.height;
  67. }
  68. - (void)setSelectedIndexByComponentID:(NSString *)componentID {
  69. NSArray* children = self.pendingChildViewControllers ?: self.childViewControllers;
  70. for (id child in children) {
  71. UIViewController<RNNLayoutProtocol>* vc = child;
  72. if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] && [vc.layoutInfo.componentId isEqualToString:componentID]) {
  73. NSUInteger selectedIndex = [children indexOfObject:child];
  74. [self setSelectedIndex:selectedIndex];
  75. _currentTabIndex = selectedIndex;
  76. }
  77. }
  78. }
  79. - (void)setSelectedIndex:(NSUInteger)selectedIndex {
  80. _currentTabIndex = selectedIndex;
  81. [super setSelectedIndex:selectedIndex];
  82. }
  83. - (UIViewController *)selectedViewController {
  84. NSArray* children = self.pendingChildViewControllers ?: self.childViewControllers;
  85. return children.count ? children[_currentTabIndex] : nil;
  86. }
  87. - (void)loadChildren:(NSArray *)children {
  88. if (self.viewWillAppearOnce) {
  89. [super loadChildren:children];
  90. self.pendingChildViewControllers = nil;
  91. }
  92. }
  93. #pragma mark UITabBarControllerDelegate
  94. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  95. [self.eventEmitter sendBottomTabSelected:@(tabBarController.selectedIndex) unselected:@(_currentTabIndex)];
  96. _currentTabIndex = tabBarController.selectedIndex;
  97. }
  98. - (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer {
  99. if (recognizer.state == UIGestureRecognizerStateBegan) {
  100. NSUInteger _index = [self.tabBar.subviews indexOfObject:(UIView *)recognizer.view];
  101. [self.eventEmitter sendBottomTabLongPressed:@(_index)];
  102. }
  103. }
  104. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
  105. {
  106. NSUInteger _index = [tabBarController.viewControllers indexOfObject:viewController];
  107. [self.eventEmitter sendBottomTabPressed:@(_index)];
  108. if([[viewController resolveOptions].bottomTab.selectTabOnPress getWithDefaultValue:YES]){
  109. return YES;
  110. }
  111. return NO;
  112. }
  113. # pragma mark - UIViewController overrides
  114. - (void)willMoveToParentViewController:(UIViewController *)parent {
  115. [self.presenter willMoveToParentViewController:parent];
  116. }
  117. - (UIStatusBarStyle)preferredStatusBarStyle {
  118. return [self.presenter getStatusBarStyle];
  119. }
  120. - (BOOL)prefersStatusBarHidden {
  121. return [self.presenter getStatusBarVisibility];
  122. }
  123. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  124. return [self.presenter getOrientation];
  125. }
  126. - (BOOL)hidesBottomBarWhenPushed {
  127. return [self.presenter hidesBottomBarWhenPushed];
  128. }
  129. @end