react-native-navigation的迁移库

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #import "RNNAnimator.h"
  2. #import "RNNTransition.h"
  3. @interface RNNAnimator()
  4. @property (nonatomic, strong) RNNAnimationOptions* transitionOptions;
  5. @property (nonatomic) BOOL backButton;
  6. @property (nonatomic, weak) UIViewController* fromVC;
  7. @property (nonatomic, weak) UIViewController* toVC;
  8. @end
  9. @implementation RNNAnimator
  10. -(instancetype)initWithTransitionOptions:(RNNAnimationOptions *)transitionOptions {
  11. self = [super init];
  12. if (transitionOptions.animations) {
  13. [self setupTransition:transitionOptions];
  14. } else {
  15. return nil;
  16. }
  17. return self;
  18. }
  19. -(void)setupTransition:(RNNAnimationOptions *)transitionOptions {
  20. self.transitionOptions = transitionOptions;
  21. if (!transitionOptions.animations) {
  22. [[NSException exceptionWithName:NSInvalidArgumentException reason:@"No animations" userInfo:nil] raise];
  23. }
  24. self.backButton = false;
  25. }
  26. -(NSArray*)prepareSharedElementTransitionWithComponentView:(UIView*)componentView {
  27. NSMutableArray* transitions = [NSMutableArray new];
  28. for (NSDictionary* transition in self.transitionOptions.animations) {
  29. RNNTransitionStateHolder* transitionStateHolder = [[RNNTransitionStateHolder alloc] initWithDict:transition];
  30. RNNTransition* transition = [[RNNTransition alloc] initFromVC:self.fromVC toVC:self.toVC transitionOptions:transitionStateHolder isBackButton:self.backButton];
  31. [componentView addSubview:transition.animatedView];
  32. [componentView bringSubviewToFront:transition.animatedView];
  33. [transitions addObject:transition];
  34. }
  35. return transitions;
  36. }
  37. -(void)animateTransitions:(NSArray*)transitions {
  38. for (RNNTransition* transition in transitions ) {
  39. [transition animate];
  40. }
  41. }
  42. -(void)animateCompletion:(NSArray*)transitions fromVCSnapshot:(UIView*)fromSnapshot andTransitioningContext:(id<UIViewControllerContextTransitioning>)transitionContext {
  43. [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:[self.transitionOptions.springDamping doubleValue] initialSpringVelocity:[self.transitionOptions.springVelocity doubleValue] options:UIViewAnimationOptionCurveEaseOut animations:^{
  44. self.toVC.view.alpha = 1;
  45. } completion:^(BOOL finished) {
  46. for (RNNTransition* transition in transitions ) {
  47. [transition transitionCompleted];
  48. }
  49. [fromSnapshot removeFromSuperview];
  50. if (![transitionContext transitionWasCancelled]) {
  51. self.toVC.view.alpha = 1;
  52. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  53. self.backButton = true;
  54. }
  55. }];
  56. }
  57. - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
  58. return [self.transitionOptions.duration doubleValue];
  59. }
  60. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
  61. UIViewController* toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  62. UIViewController* fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  63. UIView* componentView = [transitionContext containerView];
  64. self.fromVC = fromVC;
  65. self.toVC = toVC;
  66. toVC.view.frame = fromVC.view.frame;
  67. UIView* fromSnapshot = [fromVC.view snapshotViewAfterScreenUpdates:true];
  68. fromSnapshot.frame = fromVC.view.frame;
  69. [componentView addSubview:fromSnapshot];
  70. [componentView addSubview:toVC.view];
  71. toVC.view.alpha = 0;
  72. NSArray* transitions = [self prepareSharedElementTransitionWithComponentView:componentView];
  73. [self animateCompletion:transitions fromVCSnapshot:fromSnapshot andTransitioningContext:transitionContext];
  74. [self animateTransitions:transitions];
  75. }
  76. @end