react-native-navigation的迁移库

RNNBottomTabsController.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. [_bottomTabPresenter mergeOptions:options resolvedOptions:child.resolveOptions child:[self findViewController:child]];
  36. [_dotIndicatorPresenter mergeOptions:options resolvedOptions:child.resolveOptions child:[self findViewController:child]];
  37. }
  38. - (id<UITabBarControllerDelegate>)delegate {
  39. return self;
  40. }
  41. - (void)render {
  42. [_bottomTabsAttacher attach:self];
  43. }
  44. - (void)viewDidLayoutSubviews {
  45. [self.presenter viewDidLayoutSubviews];
  46. for (UIView *view in [[self tabBar] subviews]) {
  47. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleLongPress:)];
  48. if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) {
  49. [view addGestureRecognizer: longPressGesture];
  50. }
  51. }
  52. [_dotIndicatorPresenter bottomTabsDidLayoutSubviews:self];
  53. }
  54. - (UIViewController *)getCurrentChild {
  55. return self.selectedViewController;
  56. }
  57. - (CGFloat)getBottomTabsHeight {
  58. return self.tabBar.frame.size.height;
  59. }
  60. - (void)setSelectedIndexByComponentID:(NSString *)componentID {
  61. for (id child in self.childViewControllers) {
  62. UIViewController<RNNLayoutProtocol>* vc = child;
  63. if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] && [vc.layoutInfo.componentId isEqualToString:componentID]) {
  64. [self setSelectedIndex:[self.childViewControllers indexOfObject:child]];
  65. }
  66. }
  67. }
  68. - (void)setSelectedIndex:(NSUInteger)selectedIndex {
  69. _currentTabIndex = selectedIndex;
  70. [super setSelectedIndex:selectedIndex];
  71. }
  72. - (UIStatusBarStyle)preferredStatusBarStyle {
  73. return [[self presenter] getStatusBarStyle:self.resolveOptions];
  74. }
  75. #pragma mark UITabBarControllerDelegate
  76. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  77. [self.eventEmitter sendBottomTabSelected:@(tabBarController.selectedIndex) unselected:@(_currentTabIndex)];
  78. _currentTabIndex = tabBarController.selectedIndex;
  79. }
  80. - (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer {
  81. if (recognizer.state == UIGestureRecognizerStateBegan) {
  82. NSUInteger _index = [self.tabBar.subviews indexOfObject:(UIView *)recognizer.view];
  83. [self.eventEmitter sendBottomTabLongPressed:@(_index)];
  84. }
  85. }
  86. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
  87. {
  88. NSUInteger _index = [tabBarController.viewControllers indexOfObject:viewController];
  89. [self.eventEmitter sendBottomTabPressed:@(_index)];
  90. if([[viewController resolveOptions].bottomTab.selectTabOnPress getWithDefaultValue:YES]){
  91. return YES;
  92. }
  93. return NO;
  94. }
  95. @end