react-native-navigation的迁移库

RNNNavigationController.m 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.options mergeInOptions:self.getCurrentChild.resolveOptions.copy] 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 *)childViewControllerForStatusBarStyle {
  92. return self.topViewController;
  93. }
  94. - (void)setTopBarBackgroundColor:(UIColor *)backgroundColor {
  95. if (backgroundColor) {
  96. CGFloat bgColorAlpha = CGColorGetAlpha(backgroundColor.CGColor);
  97. if (bgColorAlpha == 0.0) {
  98. if (![self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
  99. [self storeOriginalTopBarImages:self];
  100. UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
  101. transparentView.backgroundColor = [UIColor clearColor];
  102. transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
  103. [self.navigationBar insertSubview:transparentView atIndex:0];
  104. }
  105. self.navigationBar.translucent = YES;
  106. [self.navigationBar setBackgroundColor:[UIColor clearColor]];
  107. self.navigationBar.shadowImage = [UIImage new];
  108. [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  109. } else {
  110. self.navigationBar.barTintColor = backgroundColor;
  111. UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  112. if (transparentView){
  113. [transparentView removeFromSuperview];
  114. [self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
  115. self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
  116. self.originalTopBarImages = nil;
  117. }
  118. }
  119. } else {
  120. UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
  121. if (transparentView){
  122. [transparentView removeFromSuperview];
  123. [self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] ? self.originalTopBarImages[@"backgroundImage"] : [self.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
  124. self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"] ? self.originalTopBarImages[@"shadowImage"] : self.navigationBar.shadowImage;
  125. self.originalTopBarImages = nil;
  126. }
  127. self.navigationBar.barTintColor = nil;
  128. }
  129. }
  130. - (void)storeOriginalTopBarImages:(UINavigationController *)navigationController {
  131. NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
  132. UIImage *bgImage = [navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
  133. if (bgImage != nil) {
  134. originalTopBarImages[@"backgroundImage"] = bgImage;
  135. }
  136. UIImage *shadowImage = navigationController.navigationBar.shadowImage;
  137. if (shadowImage != nil) {
  138. originalTopBarImages[@"shadowImage"] = shadowImage;
  139. }
  140. self.originalTopBarImages = originalTopBarImages;
  141. }
  142. @end