react-native-navigation的迁移库

RNNNavigationController.m 6.7KB

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