react-native-navigation的迁移库

RNNNavigationStackManager.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #import "RNNNavigationStackManager.h"
  2. #import "RNNErrorHandler.h"
  3. #import <React/RCTI18nUtil.h>
  4. typedef void (^RNNAnimationBlock)(void);
  5. @implementation RNNNavigationStackManager
  6. - (void)push:(UIViewController *)newTop onTop:(UIViewController *)onTopViewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  7. UINavigationController *nvc = onTopViewController.navigationController;
  8. if([[RCTI18nUtil sharedInstance] isRTL]) {
  9. nvc.view.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
  10. nvc.navigationBar.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
  11. } else {
  12. nvc.view.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
  13. nvc.navigationBar.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
  14. }
  15. [self performAnimationBlock:^{
  16. [nvc pushViewController:newTop animated:animated];
  17. } completion:completion];
  18. }
  19. - (void)pop:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  20. UINavigationController *nvc = viewController.navigationController;
  21. if ([nvc.viewControllers indexOfObject:viewController] < 0) {
  22. [RNNErrorHandler reject:rejection withErrorCode:1012 errorDescription:@"popping component failed"];
  23. return;
  24. }
  25. if ([nvc topViewController] != viewController) {
  26. NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
  27. [vcs removeObject:viewController];
  28. [self performAnimationBlock:^{
  29. [nvc setViewControllers:vcs animated:animated];
  30. } completion:^{
  31. completion();
  32. }];
  33. } else {
  34. [self performAnimationBlock:^{
  35. [viewController.navigationController popViewControllerAnimated:animated];
  36. } completion:^{
  37. completion();
  38. }];
  39. }
  40. }
  41. - (void)popTo:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNPopCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection; {
  42. __block NSArray* poppedVCs;
  43. if ([viewController.navigationController.childViewControllers containsObject:viewController]) {
  44. [self performAnimationBlock:^{
  45. poppedVCs = [viewController.navigationController popToViewController:viewController animated:animated];
  46. } completion:^{
  47. if (completion) {
  48. completion(poppedVCs);
  49. }
  50. }];
  51. } else {
  52. [RNNErrorHandler reject:rejection withErrorCode:1011 errorDescription:@"component not found in stack"];
  53. }
  54. }
  55. - (void)popToRoot:(UIViewController*)viewController animated:(BOOL)animated completion:(RNNPopCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  56. __block NSArray* poppedVCs;
  57. [self performAnimationBlock:^{
  58. poppedVCs = [viewController.navigationController popToRootViewControllerAnimated:animated];
  59. } completion:^{
  60. completion(poppedVCs);
  61. }];
  62. }
  63. - (void)setStackChildren:(NSArray<UIViewController *> *)children fromViewController:(UIViewController *)fromViewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  64. UINavigationController* nvc = fromViewController.navigationController;
  65. [self performAnimationBlock:^{
  66. [nvc setViewControllers:children animated:animated];
  67. } completion:completion];
  68. }
  69. # pragma mark Private
  70. - (void)performAnimationBlock:(RNNAnimationBlock)animationBlock completion:(RNNTransitionCompletionBlock)completion {
  71. [CATransaction begin];
  72. [CATransaction setCompletionBlock:^{
  73. if (completion) {
  74. completion();
  75. }
  76. }];
  77. animationBlock();
  78. [CATransaction commit];
  79. }
  80. @end