react-native-navigation的迁移库

UINavigationController+RNNCommands.m 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #import "UINavigationController+RNNCommands.h"
  2. #import "RNNErrorHandler.h"
  3. #import <React/RCTI18nUtil.h>
  4. typedef void (^RNNAnimationBlock)(void);
  5. @implementation UINavigationController (RNNCommands)
  6. - (void)push:(UIViewController *)newTop onTop:(UIViewController *)onTopViewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
  7. if([[RCTI18nUtil sharedInstance] isRTL]) {
  8. self.view.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
  9. self.navigationBar.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
  10. } else {
  11. self.view.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
  12. self.navigationBar.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
  13. }
  14. [self performAnimationBlock:^{
  15. [self pushViewController:newTop animated:animated];
  16. } completion:completion];
  17. }
  18. - (void)pop:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection {
  19. if ([self.viewControllers indexOfObject:viewController] < 0) {
  20. [RNNErrorHandler reject:rejection withErrorCode:1012 errorDescription:@"popping component failed"];
  21. return;
  22. }
  23. if ([self topViewController] != viewController) {
  24. NSMutableArray * vcs = self.viewControllers.mutableCopy;
  25. [vcs removeObject:viewController];
  26. [self performAnimationBlock:^{
  27. [self setViewControllers:vcs animated:animated];
  28. } completion:^{
  29. completion();
  30. }];
  31. } else {
  32. [self performAnimationBlock:^{
  33. [self popViewControllerAnimated:animated];
  34. } completion:^{
  35. completion();
  36. }];
  37. }
  38. }
  39. - (void)popTo:(UIViewController *)viewController animated:(BOOL)animated completion:(RNNPopCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)rejection; {
  40. __block NSArray* poppedVCs;
  41. if ([self.childViewControllers containsObject:viewController]) {
  42. [self performAnimationBlock:^{
  43. poppedVCs = [self 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 = [self 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. [self performAnimationBlock:^{
  63. [self setViewControllers:children animated:animated];
  64. } completion:completion];
  65. }
  66. # pragma mark Private
  67. - (void)performAnimationBlock:(RNNAnimationBlock)animationBlock completion:(RNNTransitionCompletionBlock)completion {
  68. [CATransaction begin];
  69. [CATransaction setCompletionBlock:^{
  70. if (completion) {
  71. completion();
  72. }
  73. }];
  74. animationBlock();
  75. [CATransaction commit];
  76. }
  77. @end