react-native-navigation的迁移库

RNNTransition.m 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #import "RNNTransition.h"
  2. #import "RNNElementFinder.h"
  3. #import "RNNTransitionStateHolder.h"
  4. #import "RNNViewLocation.h"
  5. #import "RNNInteractivePopAnimator.h"
  6. @interface RNNTransition () {
  7. UIViewController* _toVC;
  8. UIViewController* _fromVC;
  9. BOOL _isBackButton;
  10. }
  11. @property (nonatomic, strong) RNNElementView* fromElement;
  12. @property (nonatomic, strong) RNNElementView* toElement;
  13. @property (nonatomic, strong) RNNViewLocation* locations;
  14. @end
  15. @implementation RNNTransition
  16. - (instancetype)initFromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC transitionOptions:(RNNTransitionStateHolder *)transitionOptions isBackButton:(BOOL)isBackButton {
  17. self = [super init];
  18. _toVC = toVC;
  19. _fromVC = fromVC;
  20. _isBackButton = isBackButton;
  21. self.options = transitionOptions;
  22. RNNElementFinder* elementFinder = [[RNNElementFinder alloc] initWithToVC:toVC andfromVC:fromVC];
  23. self.fromElement = [elementFinder findElementForId:self.options.fromId];
  24. self.toElement = [elementFinder findElementForId:transitionOptions.toId];
  25. self.locations = [[RNNViewLocation alloc] initWithFromElement:self.fromElement toElement:self.toElement startPoint:CGPointMake(self.options.startX, self.options.startY) endPoint:CGPointMake(self.options.endX, self.options.endY) andVC:fromVC];
  26. self.animatedView = [[RNNAnimatedView alloc] initFromElement:self.fromElement toElement:self.toElement andLocation:self.locations andIsBackButton:isBackButton startAlpha:self.options.startAlpha endAlpha:self.options.endAlpha];
  27. if (transitionOptions.isSharedElementTransition) {
  28. [self.toElement setHidden: YES];
  29. }
  30. [self.fromElement setHidden:YES];
  31. return self;
  32. }
  33. - (void)animate {
  34. [UIView animateWithDuration:self.options.duration delay:self.options.startDelay usingSpringWithDamping:self.options.springDamping initialSpringVelocity:self.options.springVelocity options:UIViewAnimationOptionCurveEaseOut animations:^{
  35. [self setAnimatedViewFinalProperties];
  36. } completion:^(BOOL finished) {
  37. }];
  38. }
  39. - (void)setAnimatedViewFinalProperties {
  40. CGFloat alpha = _isBackButton ? self.options.startAlpha : self.options.endAlpha;
  41. self.animatedView.alpha = alpha;
  42. CGPoint center = _isBackButton ? self.locations.fromCenter : self.locations.toCenter;
  43. self.animatedView.center = center;
  44. CGAffineTransform transform = _isBackButton ? self.locations.transformBack : self.locations.transform;
  45. self.animatedView.transform = transform;
  46. RNNElementView* fromElement = _isBackButton ? self.toElement : self.fromElement;
  47. RNNElementView* toElement = _isBackButton ? self.fromElement : self.toElement;
  48. if (self.options.isSharedElementTransition) {
  49. if ([[fromElement subviews][0] isKindOfClass:[UIImageView class]]) {
  50. self.animatedView.contentMode = UIViewContentModeScaleAspectFill;
  51. if ([toElement resizeMode]){
  52. self.animatedView.contentMode = [RNNAnimatedView contentModefromString:[toElement resizeMode]];
  53. }
  54. }
  55. }
  56. }
  57. - (void)transitionCompleted {
  58. [self.fromElement setHidden:NO];
  59. if (self.options.isSharedElementTransition) {
  60. [self.toElement setHidden:NO];
  61. }
  62. [self.animatedView removeFromSuperview];
  63. if (self.options.interactivePop) {
  64. RNNInteractivePopAnimator* interactivePopAnimator = [[RNNInteractivePopAnimator alloc] initWithTopView:self.toElement andBottomView:self.fromElement andOriginFrame:self.locations.fromFrame andViewController:_toVC];
  65. UIPanGestureRecognizer* gesture = [[UIPanGestureRecognizer alloc] initWithTarget:interactivePopAnimator
  66. action:@selector(handleGesture:)];
  67. [self.toElement addGestureRecognizer:gesture];
  68. }
  69. }
  70. @end