react-native-navigation的迁移库

RNNAnimator.m 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, strong) UIViewController* fromVC;
  7. @property (nonatomic, strong) UIViewController* toVC;
  8. @end
  9. @implementation RNNAnimator
  10. -(instancetype)initWithTransitionOptions:(RNNAnimationOptions *)transitionOptions {
  11. self = [super init];
  12. if (transitionOptions) {
  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] initWithTransition: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. [UIView animateWithDuration:transition.options.duration delay:transition.options.startDelay usingSpringWithDamping:transition.options.springDamping initialSpringVelocity:transition.options.springVelocity options:UIViewAnimationOptionCurveEaseOut animations:^{
  40. [transition setAnimatedViewFinalProperties];
  41. } completion:^(BOOL finished) {
  42. }];
  43. }
  44. }
  45. -(void)animateCompletion:(NSArray*)transitions fromVCSnapshot:(UIView*)fromSnapshot andTransitioningContext:(id<UIViewControllerContextTransitioning>)transitionContext {
  46. [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:[self.transitionOptions.springDamping doubleValue] initialSpringVelocity:[self.transitionOptions.springVelocity doubleValue] options:UIViewAnimationOptionCurveEaseOut animations:^{
  47. self.toVC.view.alpha = 1;
  48. } completion:^(BOOL finished) {
  49. for (RNNTransition* transition in transitions ) {
  50. [transition transitionCompleted];
  51. }
  52. [fromSnapshot removeFromSuperview];
  53. if (![transitionContext transitionWasCancelled]) {
  54. self.toVC.view.alpha = 1;
  55. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  56. self.backButton = true;
  57. }
  58. }];
  59. }
  60. - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
  61. return [self.transitionOptions.duration doubleValue];
  62. }
  63. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
  64. UIViewController* toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  65. UIViewController* fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  66. UIView* componentView = [transitionContext containerView];
  67. self.fromVC = fromVC;
  68. self.toVC = toVC;
  69. toVC.view.frame = fromVC.view.frame;
  70. UIView* fromSnapshot = [fromVC.view snapshotViewAfterScreenUpdates:true];
  71. fromSnapshot.frame = fromVC.view.frame;
  72. [componentView addSubview:fromSnapshot];
  73. [componentView addSubview:toVC.view];
  74. toVC.view.alpha = 0;
  75. NSArray* transitions = [self prepareSharedElementTransitionWithComponentView:componentView];
  76. [self animateCompletion:transitions fromVCSnapshot:fromSnapshot andTransitioningContext:transitionContext];
  77. [self animateTransitions:transitions];
  78. }
  79. @end