react-native-navigation的迁移库

RNNBottomTabsController.m 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #import "RNNBottomTabsController.h"
  2. #import "UITabBarController+RNNUtils.h"
  3. @implementation RNNBottomTabsController {
  4. NSUInteger _currentTabIndex;
  5. BottomTabsBaseAttacher* _bottomTabsAttacher;
  6. }
  7. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
  8. creator:(id<RNNComponentViewCreator>)creator
  9. options:(RNNNavigationOptions *)options
  10. defaultOptions:(RNNNavigationOptions *)defaultOptions
  11. presenter:(RNNBasePresenter *)presenter
  12. eventEmitter:(RNNEventEmitter *)eventEmitter
  13. childViewControllers:(NSArray *)childViewControllers
  14. bottomTabsAttacher:(BottomTabsBaseAttacher *)bottomTabsAttacher {
  15. self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter childViewControllers:childViewControllers];
  16. _bottomTabsAttacher = bottomTabsAttacher;
  17. return self;
  18. }
  19. - (id<UITabBarControllerDelegate>)delegate {
  20. return self;
  21. }
  22. - (void)render {
  23. [_bottomTabsAttacher attach:self];
  24. }
  25. - (void)viewDidLayoutSubviews {
  26. [self.presenter viewDidLayoutSubviews];
  27. for (UIView *view in [[self tabBar] subviews]) {
  28. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleLongPress:)];
  29. if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) {
  30. [view addGestureRecognizer: longPressGesture];
  31. }
  32. }
  33. }
  34. - (UIViewController *)getCurrentChild {
  35. return self.selectedViewController;
  36. }
  37. - (CGFloat)getBottomTabsHeight {
  38. return self.tabBar.frame.size.height;
  39. }
  40. - (void)setSelectedIndexByComponentID:(NSString *)componentID {
  41. for (id child in self.childViewControllers) {
  42. UIViewController<RNNLayoutProtocol>* vc = child;
  43. if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] && [vc.layoutInfo.componentId isEqualToString:componentID]) {
  44. [self setSelectedIndex:[self.childViewControllers indexOfObject:child]];
  45. }
  46. }
  47. }
  48. - (void)setSelectedIndex:(NSUInteger)selectedIndex {
  49. _currentTabIndex = selectedIndex;
  50. [super setSelectedIndex:selectedIndex];
  51. }
  52. - (UIStatusBarStyle)preferredStatusBarStyle {
  53. return [[self presenter] getStatusBarStyle:self.resolveOptions];
  54. }
  55. #pragma mark UITabBarControllerDelegate
  56. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  57. [self.eventEmitter sendBottomTabSelected:@(tabBarController.selectedIndex) unselected:@(_currentTabIndex)];
  58. _currentTabIndex = tabBarController.selectedIndex;
  59. }
  60. - (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer {
  61. if (recognizer.state == UIGestureRecognizerStateBegan) {
  62. NSUInteger _index = [self.tabBar.subviews indexOfObject:(UIView *)recognizer.view];
  63. [self.eventEmitter sendBottomTabLongPressed:@(_index)];
  64. }
  65. }
  66. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
  67. {
  68. NSUInteger _index = [tabBarController.viewControllers indexOfObject:viewController];
  69. [self.eventEmitter sendBottomTabPressed:@(_index)];
  70. if([[viewController resolveOptions].bottomTab.selectTabOnPress getWithDefaultValue:YES]){
  71. return YES;
  72. }
  73. return NO;
  74. }
  75. @end