react-native-navigation的迁移库

TransitionDelegate.m 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #import "TransitionDelegate.h"
  2. #import "DisplayLinkAnimator.h"
  3. @implementation TransitionDelegate {
  4. RCTUIManager* _uiManager;
  5. id <UIViewControllerContextTransitioning> _transitionContext;
  6. BOOL _animate;
  7. }
  8. - (instancetype)initWithUIManager:(RCTUIManager *)uiManager {
  9. self = [super init];
  10. _uiManager = uiManager;
  11. [_uiManager.observerCoordinator addObserver:self];
  12. return self;
  13. }
  14. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
  15. _animate = YES;
  16. _transitionContext = transitionContext;
  17. [self prepareTransitionContext:transitionContext];
  18. UIViewController* fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  19. if (![fromVC.navigationController.childViewControllers containsObject:fromVC]) {
  20. [self performAnimationOnce];
  21. }
  22. }
  23. - (void)prepareTransitionContext:(id<UIViewControllerContextTransitioning>)transitionContext {
  24. UIViewController* toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  25. toVC.view.alpha = 0;
  26. [transitionContext.containerView addSubview:toVC.view];
  27. }
  28. - (void)performAnimationOnce {
  29. if (_animate) {
  30. _animate = NO;
  31. RCTExecuteOnMainQueue(^{
  32. id<UIViewControllerContextTransitioning> transitionContext = self->_transitionContext;
  33. UIViewController* fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  34. UIViewController* toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  35. NSArray* transitions = [self createTransitionsFromVC:fromVC toVC:toVC containerView:transitionContext.containerView];
  36. [self animateTransitions:transitions andTransitioningContext:transitionContext];
  37. });
  38. }
  39. }
  40. - (void)animateTransitions:(NSArray<id<DisplayLinkAnimatorDelegate>>*)animators andTransitioningContext:(id<UIViewControllerContextTransitioning>)transitionContext {
  41. DisplayLinkAnimator* displayLinkAnimator = [[DisplayLinkAnimator alloc] initWithDisplayLinkAnimators:animators duration:[self transitionDuration:transitionContext]];
  42. [displayLinkAnimator setCompletion:^{
  43. if (![transitionContext transitionWasCancelled]) {
  44. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  45. }
  46. }];
  47. [displayLinkAnimator start];
  48. }
  49. - (NSArray *)createTransitionsFromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC containerView:(UIView *)containerView {
  50. @throw [NSException exceptionWithName:@"Unimplemented method" reason:@"createTransitionFromVC:fromVC:toVC:containerView must be overridden by subclass" userInfo:nil];
  51. return @[];
  52. }
  53. - (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
  54. return 1;
  55. }
  56. - (void)uiManagerDidPerformMounting:(RCTUIManager *)manager {
  57. [self performAnimationOnce];
  58. }
  59. @end