react-native-navigation的迁移库

RNNDotIndicatorPresenter.m 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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)apply:(UIViewController *)child :(DotIndicatorOptions *)options {
  14. if (![options hasValue]) return;
  15. if ([options.visible isFalse]) {
  16. if ([child tabBarItem].tag > 0) [self remove:child];
  17. return;
  18. }
  19. if ([self currentIndicatorEquals:child :options]) return;
  20. if ([self hasIndicator:child]) [self remove:child];
  21. UIView *indicator = [self createIndicator:options];
  22. [child tabBarItem].tag = indicator.tag;
  23. UITabBarController *bottomTabs = [self getTabBarController:child];
  24. int index = (int) [[bottomTabs childViewControllers] indexOfObject:child];
  25. [[bottomTabs getTabView:index] addSubview:indicator];
  26. [self applyConstraints:options badge:indicator tabBar:bottomTabs index:index];
  27. }
  28. - (UIView *)createIndicator:(DotIndicatorOptions *)options {
  29. UIView * indicator = [UIView new];
  30. indicator.translatesAutoresizingMaskIntoConstraints = NO;
  31. indicator.layer.cornerRadius = [[options.size getWithDefaultValue:@6] floatValue] / 2;
  32. indicator.backgroundColor = [options.color getWithDefaultValue:[UIColor redColor]];
  33. indicator.tag = arc4random();
  34. return indicator;
  35. }
  36. - (void)applyConstraints:(DotIndicatorOptions *)options badge:(UIView *)badge tabBar:(UITabBarController *)bottomTabs index:(int)index {
  37. UIView *icon = [bottomTabs getTabIcon:index];
  38. float size = [[options.size getWithDefaultValue:@6] floatValue];
  39. [NSLayoutConstraint activateConstraints:@[
  40. [badge.leftAnchor constraintEqualToAnchor:icon.rightAnchor constant:-size / 2],
  41. [badge.topAnchor constraintEqualToAnchor:icon.topAnchor constant:-size / 2],
  42. [badge.widthAnchor constraintEqualToConstant:size],
  43. [badge.heightAnchor constraintEqualToConstant:size]
  44. ]];
  45. }
  46. - (BOOL)currentIndicatorEquals:(UIViewController *)child :(DotIndicatorOptions *)options {
  47. if (![self hasIndicator:child]) return NO;
  48. UIView *currentIndicator = [self getCurrentIndicator:child];
  49. return [[currentIndicator backgroundColor] isEqual:[options.color getWithDefaultValue:[UIColor redColor]]];
  50. }
  51. - (UIView *)getCurrentIndicator:(UIViewController *)child {
  52. UITabBarController *bottomTabs = [self getTabBarController:child];
  53. int tabIndex = (int) [[bottomTabs childViewControllers] indexOfObject:child];
  54. return [[bottomTabs getTabView:tabIndex] viewWithTag:[child tabBarItem].tag];
  55. }
  56. - (BOOL)hasIndicator:(UIViewController *)child {
  57. return [child tabBarItem].tag > 0;
  58. }
  59. - (void)remove:(UIViewController *)child {
  60. UIView *view = [[[child tabBarController] tabBar] viewWithTag:[child tabBarItem].tag];
  61. [view removeFromSuperview];
  62. [child tabBarItem].tag = -1;
  63. }
  64. - (UITabBarController *)getTabBarController:(id)viewController {
  65. return [viewController isKindOfClass:[UITabBarController class]] ? viewController : [viewController tabBarController];
  66. }
  67. @end