react-native-navigation的迁移库

RNNNavigationStackManager.m 3.0KB

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