react-native-navigation的迁移库

RNNNavigationStackManager.m 3.3KB

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