react-native-navigation的迁移库

RNNNavigationController.m 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #import "RNNNavigationController.h"
  2. #import "RNNModalAnimation.h"
  3. #import "RNNRootViewController.h"
  4. const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
  5. @interface RNNNavigationController()
  6. @property (nonatomic, strong) NSMutableDictionary* originalTopBarImages;
  7. @end
  8. @implementation RNNNavigationController
  9. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo creator:(id<RNNRootViewCreator>)creator childViewControllers:(NSArray *)childViewControllers options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions presenter:(RNNNavigationControllerPresenter *)presenter {
  10. self = [super init];
  11. self.presenter = presenter;
  12. [self.presenter bindViewController:self];
  13. self.defaultOptions = defaultOptions;
  14. self.options = options;
  15. self.layoutInfo = layoutInfo;
  16. [self setViewControllers:childViewControllers];
  17. return self;
  18. }
  19. - (void)willMoveToParentViewController:(UIViewController *)parent {
  20. if (parent) {
  21. [_presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  22. }
  23. }
  24. - (void)onChildWillAppear {
  25. [_presenter applyOptions:self.resolveOptions];
  26. [_presenter renderComponents:self.resolveOptions perform:nil];
  27. [((UIViewController<RNNParentProtocol> *)self.parentViewController) onChildWillAppear];
  28. }
  29. - (RNNNavigationOptions *)resolveOptions {
  30. return [(RNNNavigationOptions *)[self.getCurrentChild.resolveOptions.copy mergeOptions:self.options] withDefault:self.defaultOptions];
  31. }
  32. - (void)mergeOptions:(RNNNavigationOptions *)options {
  33. [_presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
  34. [((UIViewController<RNNLayoutProtocol> *)self.parentViewController) mergeOptions:options];
  35. }
  36. - (void)overrideOptions:(RNNNavigationOptions *)options {
  37. [self.options overrideOptions:options];
  38. }
  39. - (void)renderTreeAndWait:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  40. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  41. dispatch_group_t group = dispatch_group_create();
  42. for (UIViewController<RNNLayoutProtocol>* childViewController in self.childViewControllers) {
  43. dispatch_group_enter(group);
  44. dispatch_async(dispatch_get_main_queue(), ^{
  45. [childViewController renderTreeAndWait:wait perform:^{
  46. dispatch_group_leave(group);
  47. }];
  48. });
  49. }
  50. dispatch_group_enter(group);
  51. [self.presenter renderComponents:self.resolveOptions perform:^{
  52. dispatch_group_leave(group);
  53. }];
  54. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  55. dispatch_async(dispatch_get_main_queue(), ^{
  56. readyBlock();
  57. });
  58. });
  59. }
  60. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  61. return self.getCurrentChild.supportedInterfaceOrientations;
  62. }
  63. - (UINavigationController *)navigationController {
  64. return self;
  65. }
  66. - (UIStatusBarStyle)preferredStatusBarStyle {
  67. return self.getCurrentChild.preferredStatusBarStyle;
  68. }
  69. - (UIModalPresentationStyle)modalPresentationStyle {
  70. return self.getCurrentChild.modalPresentationStyle;
  71. }
  72. - (UIViewController *)popViewControllerAnimated:(BOOL)animated {
  73. if (self.viewControllers.count > 1) {
  74. UIViewController *controller = self.viewControllers[self.viewControllers.count - 2];
  75. if ([controller isKindOfClass:[RNNRootViewController class]]) {
  76. RNNRootViewController *rnnController = (RNNRootViewController *)controller;
  77. [self.presenter applyOptionsBeforePopping:rnnController.resolveOptions];
  78. }
  79. }
  80. return [super popViewControllerAnimated:animated];
  81. }
  82. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  83. return [[RNNModalAnimation alloc] initWithScreenTransition:self.getCurrentChild.resolveOptions.animations.showModal isDismiss:NO];
  84. }
  85. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  86. return [[RNNModalAnimation alloc] initWithScreenTransition:self.getCurrentChild.resolveOptions.animations.dismissModal isDismiss:YES];
  87. }
  88. - (UIViewController *)getCurrentChild {
  89. return ((UIViewController<RNNParentProtocol>*)self.topViewController);
  90. }
  91. - (UIViewController<RNNLeafProtocol> *)getCurrentLeaf {
  92. return [[self getCurrentChild] getCurrentLeaf];
  93. }
  94. - (UIViewController *)childViewControllerForStatusBarStyle {
  95. return self.topViewController;
  96. }
  97. - (void)setTopBarBackgroundColor:(UIColor *)backgroundColor {
  98. if (backgroundColor) {
  99. CGFloat bgColorAlpha = CGColorGetAlpha(backgroundColor.CGColor);
  100. if (bgColorAlpha == 0.0) {
  101. if (![self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  102. [self storeOriginalTopBarImages:self];
  103. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  104. transparentView.backgroundColor = [UIColor clearColor];
  105. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  106. [self.navigationBar insertSubview:transparentView atIndex:0];
  107. }
  108. self.navigationBar.translucent = YES;
  109. [self.navigationBar setBackgroundColor:[UIColor clearColor]];
  110. self.navigationBar.shadowImage = [UIImage new];
  111. [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  112. } else {
  113. self.navigationBar.barTintColor = backgroundColor;
  114. UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  115. if (transparentView){
  116. [transparentView removeFromSuperview];
  117. [self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  118. self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  119. self.originalTopBarImages = nil;
  120. }
  121. }
  122. } else {
  123. UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  124. if (transparentView){
  125. [transparentView removeFromSuperview];
  126. [self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] ? self.originalTopBarImages[@"backgroundImage"] : [self.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
  127. self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"] ? self.originalTopBarImages[@"shadowImage"] : self.navigationBar.shadowImage;
  128. self.originalTopBarImages = nil;
  129. }
  130. self.navigationBar.barTintColor = nil;
  131. }
  132. }
  133. - (void)storeOriginalTopBarImages:(UINavigationController *)navigationController {
  134. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  135. UIImage *bgImage = [navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  136. if (bgImage != nil) {
  137. originalTopBarImages[@"backgroundImage"] = bgImage;
  138. }
  139. UIImage *shadowImage = navigationController.navigationBar.shadowImage;
  140. if (shadowImage != nil) {
  141. originalTopBarImages[@"shadowImage"] = shadowImage;
  142. }
  143. self.originalTopBarImages = originalTopBarImages;
  144. }
  145. @end