react-native-navigation的迁移库

RNNNavigationStackManager.m 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #import "RNNNavigationStackManager.h"
  2. #import "RNNErrorHandler.h"
  3. typedef void (^RNNAnimationBlock)(void);
  4. @implementation RNNNavigationStackManager
  5. - (void)push:(UIViewController *)newTop onTop:(UIViewController *)onTopViewController animated:(BOOL)animated animationDelegate:(id)animationDelegate completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  6. UINavigationController *nvc = onTopViewController.navigationController;
  7. if (animationDelegate) {
  8. nvc.delegate = animationDelegate;
  9. } else {
  10. nvc.delegate = nil;
  11. nvc.interactivePopGestureRecognizer.delegate = nil;
  12. }
  13. [self performAnimationBlock:^{
  14. [nvc pushViewController:newTop animated:animated];
  15. } completion:completion];
  16. }
  17. - (void)pop:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  18. if (!viewController.view.window) {
  19. animated = NO;
  20. }
  21. __block UIViewController *poppedVC = nil;
  22. [self performAnimationBlock:^{
  23. poppedVC = [viewController.navigationController popViewControllerAnimated:animated];
  24. } completion:^{
  25. if (poppedVC) {
  26. completion();
  27. } else {
  28. [RNNErrorHandler reject:rejection withErrorCode:1012 errorDescription:@"popping component failed"];
  29. }
  30. }];
  31. }
  32. - (void)popTo:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNPopCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection; {
  33. __block NSArray* poppedVCs;
  34. if ([viewController.navigationController.childViewControllers containsObject:viewController]) {
  35. [self performAnimationBlock:^{
  36. poppedVCs = [viewController.navigationController popToViewController:viewController animated:animated];
  37. } completion:^{
  38. if (completion) {
  39. completion(poppedVCs);
  40. }
  41. }];
  42. } else {
  43. [RNNErrorHandler reject:rejection withErrorCode:1011 errorDescription:@"component not found in stack"];
  44. }
  45. }
  46. - (void)popToRoot:(UIViewController*)viewController animated:(BOOL)animated completion:(RNNPopCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  47. __block NSArray* poppedVCs;
  48. [self performAnimationBlock:^{
  49. poppedVCs = [viewController.navigationController popToRootViewControllerAnimated:animated];
  50. } completion:^{
  51. completion(poppedVCs);
  52. }];
  53. }
  54. - (void)setStackRoot:(UIViewController *)newRoot fromViewController:(UIViewController *)fromViewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  55. UINavigationController* nvc = fromViewController.navigationController;
  56. [self performAnimationBlock:^{
  57. [nvc setViewControllers:@[newRoot] animated:animated];
  58. } completion:completion];
  59. }
  60. # pragma mark Private
  61. - (void)performAnimationBlock:(RNNAnimationBlock)animationBlock completion:(RNNTransitionCompletionBlock)completion {
  62. [CATransaction begin];
  63. [CATransaction setCompletionBlock:^{
  64. if (completion) {
  65. completion();
  66. }
  67. }];
  68. animationBlock();
  69. [CATransaction commit];
  70. }
  71. @end