react-native-navigation的迁移库

RNNBottomTabsController.m 5.0KB

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