react-native-navigation的迁移库

RNNDotIndicatorPresenter.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #import <UIKit/UIKit.h>
  2. #import "RNNDotIndicatorPresenter.h"
  3. #import "UIViewController+LayoutProtocol.h"
  4. #import "DotIndicatorOptions.h"
  5. #import "UITabBarController+RNNUtils.h"
  6. #import "RNNNavigationOptions.h"
  7. @implementation RNNDotIndicatorPresenter
  8. -(instancetype)initWithDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  9. self = [super init];
  10. _defaultOptions = defaultOptions;
  11. return self;
  12. }
  13. - (void)bottomTabsDidLayoutSubviews:(UITabBarController *)bottomTabs {
  14. for (UIViewController* child in bottomTabs.childViewControllers) {
  15. [self applyDotIndicator:child];
  16. }
  17. }
  18. - (void)applyDotIndicator:(UIViewController *)child {
  19. [self apply:child:[child resolveOptions].bottomTab.dotIndicator];
  20. }
  21. - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)resolvedOptions child:(UIViewController *)child {
  22. RNNNavigationOptions* withDefault = (RNNNavigationOptions *) [[resolvedOptions withDefault:self.defaultOptions] overrideOptions:options];
  23. if ([options.bottomTab.dotIndicator hasValue]) {
  24. [self apply:child:withDefault.bottomTab.dotIndicator];
  25. }
  26. }
  27. - (void)apply:(UIViewController *)child :(DotIndicatorOptions *)options {
  28. if (![options hasValue]) return;
  29. if ([options.visible isFalse]) {
  30. if ([child tabBarItem].tag > 0) [self remove:child];
  31. return;
  32. }
  33. if ([self currentIndicatorEquals:child :options]) return;
  34. if ([self hasIndicator:child]) [self remove:child];
  35. UIView *indicator = [self createIndicator:options];
  36. [child tabBarItem].tag = indicator.tag;
  37. UITabBarController *bottomTabs = [self getTabBarController:child];
  38. int index = (int) [[bottomTabs childViewControllers] indexOfObject:child];
  39. [[bottomTabs getTabView:index] addSubview:indicator];
  40. [self applyConstraints:options badge:indicator tabBar:bottomTabs index:index];
  41. }
  42. - (UIView *)createIndicator:(DotIndicatorOptions *)options {
  43. UIView * indicator = [UIView new];
  44. indicator.translatesAutoresizingMaskIntoConstraints = NO;
  45. indicator.layer.cornerRadius = [[options.size getWithDefaultValue:@6] floatValue] / 2;
  46. indicator.backgroundColor = [options.color getWithDefaultValue:[UIColor redColor]];
  47. indicator.tag = arc4random();
  48. return indicator;
  49. }
  50. - (void)applyConstraints:(DotIndicatorOptions *)options badge:(UIView *)badge tabBar:(UITabBarController *)bottomTabs index:(int)index {
  51. UIView *icon = [bottomTabs getTabIcon:index];
  52. float size = [[options.size getWithDefaultValue:@6] floatValue];
  53. [NSLayoutConstraint activateConstraints:@[
  54. [badge.leftAnchor constraintEqualToAnchor:icon.rightAnchor constant:-size / 2],
  55. [badge.topAnchor constraintEqualToAnchor:icon.topAnchor constant:-size / 2],
  56. [badge.widthAnchor constraintEqualToConstant:size],
  57. [badge.heightAnchor constraintEqualToConstant:size]
  58. ]];
  59. }
  60. - (BOOL)currentIndicatorEquals:(UIViewController *)child :(DotIndicatorOptions *)options {
  61. if (![self hasIndicator:child]) return NO;
  62. UIView *currentIndicator = [self getCurrentIndicator:child];
  63. return [[currentIndicator backgroundColor] isEqual:[options.color getWithDefaultValue:[UIColor redColor]]];
  64. }
  65. - (UIView *)getCurrentIndicator:(UIViewController *)child {
  66. UITabBarController *bottomTabs = [self getTabBarController:child];
  67. int tabIndex = (int) [[bottomTabs childViewControllers] indexOfObject:child];
  68. return [[bottomTabs getTabView:tabIndex] viewWithTag:[child tabBarItem].tag];
  69. }
  70. - (BOOL)hasIndicator:(UIViewController *)child {
  71. return [child tabBarItem].tag > 0;
  72. }
  73. - (void)remove:(UIViewController *)child {
  74. UIView *view = [[[child tabBarController] tabBar] viewWithTag:[child tabBarItem].tag];
  75. [view removeFromSuperview];
  76. [child tabBarItem].tag = -1;
  77. }
  78. - (UITabBarController *)getTabBarController:(id)viewController {
  79. return [viewController isKindOfClass:[UITabBarController class]] ? viewController : [viewController tabBarController];
  80. }
  81. @end