react-native-navigation的迁移库

RNNInteractivePopAnimator.m 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #import "RNNInteractivePopAnimator.h"
  2. #import "StackTransitionDelegate.h"
  3. #import "RNNElementView.h"
  4. #import "RNNComponentViewController.h"
  5. #import "VICMAImageView.h"
  6. @interface RNNInteractivePopAnimator()
  7. @property (nonatomic) CGRect topFrame;
  8. @property (nonatomic) CGFloat percent;
  9. @property (nonatomic) UINavigationController* nc;
  10. @property (nonatomic) UIView* imageSnapshot;
  11. @property (nonatomic) double totalTranslate;
  12. @property (nonatomic) id transitionContext;
  13. @end
  14. @implementation RNNInteractivePopAnimator
  15. -(instancetype)initWithTopView:(RNNElementView*)topView andBottomView:(RNNElementView*)bottomView andOriginFrame:(CGRect)originFrame andViewController:(UIViewController*)vc{
  16. RNNInteractivePopAnimator* interactiveController = [[RNNInteractivePopAnimator alloc] init];
  17. [interactiveController setTopView:topView];
  18. [interactiveController setBottomView:bottomView];
  19. [interactiveController setOriginFrame:originFrame];
  20. [interactiveController setVc:vc];
  21. [interactiveController setNc:vc.navigationController];
  22. return interactiveController;
  23. }
  24. -(void)startInteractiveTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
  25. [super startInteractiveTransition:transitionContext];
  26. }
  27. -(BOOL)shouldBeginInteractivePop:(CGPoint)velocity {
  28. if (velocity.y > 0) {
  29. return YES;
  30. }
  31. return NO;
  32. }
  33. -(BOOL)shouldCancelInteractivePop:(UIPanGestureRecognizer*)recognizer {
  34. return ([recognizer velocityInView:self.imageSnapshot].y < 0 || self.totalTranslate < 0);
  35. }
  36. -(void)handleGesture:(UIPanGestureRecognizer*)recognizer {
  37. CGPoint translation = [recognizer translationInView:self.topView];
  38. if (recognizer.state == UIGestureRecognizerStateBegan) {
  39. CGPoint velocity = [recognizer velocityInView:recognizer.view];
  40. self.nc.delegate = self;
  41. if ([self shouldBeginInteractivePop:velocity]) {
  42. [self.nc popViewControllerAnimated:YES];
  43. }
  44. } else
  45. if (recognizer.state == UIGestureRecognizerStateChanged) {
  46. self.totalTranslate = self.totalTranslate + translation.y;
  47. [self animateAlongsideTransition:^void(id context){
  48. self.imageSnapshot.center = CGPointMake(self.imageSnapshot.center.x + translation.x,
  49. self.imageSnapshot.center.y + translation.y);
  50. }
  51. completion:nil];
  52. [recognizer setTranslation:CGPointMake(0, 0) inView:self.imageSnapshot];
  53. if (self.totalTranslate >= 0 && self.totalTranslate <= 400.0) {
  54. [self updateInteractiveTransition:self.totalTranslate/400];
  55. }
  56. } else if (recognizer.state == UIGestureRecognizerStateEnded) {
  57. if([self shouldCancelInteractivePop:recognizer]) {
  58. [self cancelInteractiveTransition];
  59. [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.8 options:UIViewAnimationOptionCurveEaseOut animations:^{
  60. self.imageSnapshot.frame = self.topFrame;
  61. } completion:^(BOOL finished) {
  62. self.nc.delegate = (RNNComponentViewController*)self.vc;
  63. }];
  64. } else {
  65. [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.8 options:UIViewAnimationOptionCurveEaseOut animations:^{
  66. self.imageSnapshot.frame = self.originFrame;
  67. self.imageSnapshot.contentMode = UIViewContentModeScaleAspectFill;
  68. } completion:^(BOOL finished) {
  69. self.nc.delegate = nil;
  70. }];
  71. [self finishInteractiveTransition];
  72. }
  73. }
  74. }
  75. - (BOOL)animateAlongsideTransition:(void (^)(id<UIViewControllerTransitionCoordinatorContext> context))animation
  76. completion:(void (^)(id<UIViewControllerTransitionCoordinatorContext> context))completion;{
  77. animation(nil);
  78. return YES;
  79. }
  80. - (void)animationEnded:(BOOL)transitionCompleted {
  81. }
  82. - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
  83. {
  84. return 0.7;
  85. }
  86. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
  87. {
  88. // self.totalTranslate = 0;
  89. // self.transitionContext = transitionContext;
  90. // UIViewController* toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  91. // UIViewController* fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  92. // UIView* componentView = [transitionContext containerView];
  93. //
  94. // toVC.view.frame = fromVC.view.frame;
  95. // UIView* topViewContent = self.topView.view;
  96. // UIImage* image = [[self.topView subviews][0] image];
  97. // UIView* imageSnapshot = [[VICMAImageView alloc] initWithImage:image];
  98. // CGPoint fromSharedViewFrameOrigin = [topViewContent.superview convertPoint:topViewContent.frame.origin toView:fromVC.view];
  99. // CGRect fromOriginRect = CGRectMake(fromSharedViewFrameOrigin.x, fromSharedViewFrameOrigin.y, topViewContent.frame.size.width, topViewContent.frame.size.height);
  100. // self.topFrame = fromOriginRect;
  101. // imageSnapshot.contentMode = UIViewContentModeScaleAspectFill;
  102. // imageSnapshot.frame = fromOriginRect;
  103. // self.imageSnapshot = imageSnapshot;
  104. // [self.bottomView setHidden:YES];
  105. // UIView* toSnapshot = [toVC.view snapshotViewAfterScreenUpdates:true];
  106. // toSnapshot.frame = fromVC.view.frame;
  107. // [componentView insertSubview:(UIView *)toSnapshot atIndex:1];
  108. // [componentView addSubview:self.imageSnapshot];
  109. // toSnapshot.alpha = 0.0;
  110. // [self.topView setHidden:YES];
  111. // [UIView animateKeyframesWithDuration:(NSTimeInterval)[self transitionDuration:transitionContext]
  112. // delay:0
  113. // options: UIViewKeyframeAnimationOptionAllowUserInteraction
  114. // animations:^{
  115. // [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 animations:^{
  116. // fromVC.view.alpha = 0;
  117. // toSnapshot.alpha = 1;
  118. // }];
  119. // }
  120. // completion:^(BOOL finished) {
  121. // [self.bottomView setHidden:NO];
  122. // [toSnapshot removeFromSuperview];
  123. // [self.imageSnapshot removeFromSuperview];
  124. // self.totalTranslate = 0;
  125. // if (![transitionContext transitionWasCancelled]) {
  126. // [componentView addSubview: toVC.view];
  127. // [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  128. //
  129. // }
  130. // if ([transitionContext transitionWasCancelled]) {
  131. // [self.topView setHidden:NO];
  132. // [componentView addSubview: fromVC.view];
  133. // [transitionContext completeTransition:NO];
  134. // }
  135. // }];
  136. }
  137. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  138. animationControllerForOperation:(UINavigationControllerOperation)operation
  139. fromViewController:(UIViewController*)fromVC
  140. toViewController:(UIViewController*)toVC {
  141. if (operation == UINavigationControllerOperationPop) {
  142. return self;
  143. } else {
  144. return nil;
  145. }
  146. return nil;
  147. }
  148. - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
  149. interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController {
  150. return self;
  151. }
  152. @end