react-native-navigation的迁移库

RNNNavigationStackManager.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. [self performAnimationBlock:^{
  22. [viewController.navigationController popViewControllerAnimated:animated];
  23. } completion:completion];
  24. }
  25. - (void)popTo:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNPopCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection; {
  26. __block NSArray* poppedVCs;
  27. if ([viewController.navigationController.childViewControllers containsObject:viewController]) {
  28. [self performAnimationBlock:^{
  29. poppedVCs = [viewController.navigationController popToViewController:viewController animated:animated];
  30. } completion:^{
  31. if (completion) {
  32. completion(poppedVCs);
  33. }
  34. }];
  35. } else {
  36. [RNNErrorHandler reject:rejection withErrorCode:1011 errorDescription:@"component not found in stack"];
  37. }
  38. }
  39. - (void)popToRoot:(UIViewController*)viewController animated:(BOOL)animated completion:(RNNPopCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  40. __block NSArray* poppedVCs;
  41. [self performAnimationBlock:^{
  42. poppedVCs = [viewController.navigationController popToRootViewControllerAnimated:animated];
  43. } completion:^{
  44. completion(poppedVCs);
  45. }];
  46. }
  47. - (void)setStackRoot:(UIViewController *)newRoot fromViewController:(UIViewController *)fromViewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  48. UINavigationController* nvc = fromViewController.navigationController;
  49. [self performAnimationBlock:^{
  50. [nvc setViewControllers:@[newRoot] animated:animated];
  51. } completion:completion];
  52. }
  53. # pragma mark Private
  54. - (void)performAnimationBlock:(RNNAnimationBlock)animationBlock completion:(RNNTransitionCompletionBlock)completion {
  55. [CATransaction begin];
  56. [CATransaction setCompletionBlock:^{
  57. if (completion) {
  58. completion();
  59. }
  60. }];
  61. animationBlock();
  62. [CATransaction commit];
  63. }
  64. @end