react-native-navigation的迁移库

RNNNavigationStackManager.m 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 animationDelegate:(id)animationDelegate 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. if (animationDelegate) {
  16. nvc.delegate = animationDelegate;
  17. } else {
  18. nvc.delegate = nil;
  19. }
  20. [self performAnimationBlock:^{
  21. [nvc pushViewController:newTop animated:animated];
  22. } completion:completion];
  23. }
  24. - (void)pop:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  25. if (!viewController.view.window) {
  26. animated = NO;
  27. }
  28. __block UIViewController *poppedVC = nil;
  29. [self performAnimationBlock:^{
  30. poppedVC = [viewController.navigationController popViewControllerAnimated:animated];
  31. } completion:^{
  32. if (poppedVC) {
  33. completion();
  34. } else {
  35. [RNNErrorHandler reject:rejection withErrorCode:1012 errorDescription:@"popping component failed"];
  36. }
  37. }];
  38. }
  39. - (void)popTo:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNPopCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection; {
  40. __block NSArray* poppedVCs;
  41. if ([viewController.navigationController.childViewControllers containsObject:viewController]) {
  42. [self performAnimationBlock:^{
  43. poppedVCs = [viewController.navigationController popToViewController:viewController animated:animated];
  44. } completion:^{
  45. if (completion) {
  46. completion(poppedVCs);
  47. }
  48. }];
  49. } else {
  50. [RNNErrorHandler reject:rejection withErrorCode:1011 errorDescription:@"component not found in stack"];
  51. }
  52. }
  53. - (void)popToRoot:(UIViewController*)viewController animated:(BOOL)animated completion:(RNNPopCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  54. __block NSArray* poppedVCs;
  55. [self performAnimationBlock:^{
  56. poppedVCs = [viewController.navigationController popToRootViewControllerAnimated:animated];
  57. } completion:^{
  58. completion(poppedVCs);
  59. }];
  60. }
  61. - (void)setStackChildren:(NSArray<UIViewController *> *)children fromViewController:(UIViewController *)fromViewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  62. UINavigationController* nvc = fromViewController.navigationController;
  63. [self performAnimationBlock:^{
  64. [nvc setViewControllers:children animated:animated];
  65. } completion:completion];
  66. }
  67. # pragma mark Private
  68. - (void)performAnimationBlock:(RNNAnimationBlock)animationBlock completion:(RNNTransitionCompletionBlock)completion {
  69. [CATransaction begin];
  70. [CATransaction setCompletionBlock:^{
  71. if (completion) {
  72. completion();
  73. }
  74. }];
  75. animationBlock();
  76. [CATransaction commit];
  77. }
  78. @end