react-native-navigation的迁移库

RNNBottomTabsController.m 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. - (UIStatusBarStyle)preferredStatusBarStyle {
  74. return [[self presenter] getStatusBarStyle:self.resolveOptions];
  75. }
  76. #pragma mark UITabBarControllerDelegate
  77. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  78. [self.eventEmitter sendBottomTabSelected:@(tabBarController.selectedIndex) unselected:@(_currentTabIndex)];
  79. _currentTabIndex = tabBarController.selectedIndex;
  80. }
  81. - (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer {
  82. if (recognizer.state == UIGestureRecognizerStateBegan) {
  83. NSUInteger _index = [self.tabBar.subviews indexOfObject:(UIView *)recognizer.view];
  84. [self.eventEmitter sendBottomTabLongPressed:@(_index)];
  85. }
  86. }
  87. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
  88. {
  89. NSUInteger _index = [tabBarController.viewControllers indexOfObject:viewController];
  90. [self.eventEmitter sendBottomTabPressed:@(_index)];
  91. if([[viewController resolveOptions].bottomTab.selectTabOnPress getWithDefaultValue:YES]){
  92. return YES;
  93. }
  94. return NO;
  95. }
  96. @end