react-native-navigation的迁移库

RNNDotIndicatorPresenter.m 3.0KB

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