react-native-navigation的迁移库

RNNPushAnimation.m 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #import "RNNPushAnimation.h"
  2. @implementation RNNPushAnimation
  3. - (instancetype)initWithScreenTransition:(RNNScreenTransition *)screenTransition {
  4. self = [super init];
  5. self.screenTransition = screenTransition;
  6. return self;
  7. }
  8. - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
  9. return self.screenTransition.maxDuration;
  10. }
  11. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
  12. UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  13. UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  14. [[transitionContext containerView] addSubview:fromViewController.view];
  15. [[transitionContext containerView] addSubview:toViewController.view];
  16. [CATransaction begin];
  17. [CATransaction setCompletionBlock:^{
  18. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  19. }];
  20. [self animateElement:self.screenTransition.topBar view:toViewController.navigationController.navigationBar elementName:@"topBar"];
  21. [self animateElement:self.screenTransition.content view:toViewController.view elementName:@"content"];
  22. [self animateElement:self.screenTransition.bottomTabs view:toViewController.tabBarController.tabBar elementName:@"bottomTabs"];
  23. [CATransaction commit];
  24. }
  25. - (void)animationWithKeyPath:(NSString *)keyPath from:(id)from to:(id)to duration:(CFTimeInterval)duration forView:(UIView *)view animationName:(NSString *)animationName {
  26. CABasicAnimation *animation = [CABasicAnimation animation];
  27. animation.keyPath = keyPath;
  28. animation.fromValue = from;
  29. animation.toValue = to;
  30. animation.duration = duration / 1000;
  31. [view.layer addAnimation:animation forKey:animationName];
  32. }
  33. - (void)animateElement:(ElementTransitionOptions *)element view:(UIView *)view elementName:(NSString *)elementName {
  34. [self animationWithKeyPath:@"position.x" from:@(view.layer.position.x + [element.x.from getWithDefaultValue:0]) to:@(view.layer.position.x + [element.x.to getWithDefaultValue:0]) duration:[element.x.duration getWithDefaultValue:1] forView:view animationName:@"element.position.x"];
  35. [self animationWithKeyPath:@"position.y" from:@(view.layer.position.y + [element.y.from getWithDefaultValue:0]) to:@(view.layer.position.y + [element.y.to getWithDefaultValue:0]) duration:[element.y.duration getWithDefaultValue:1] forView:view animationName:[NSString stringWithFormat:@"%@.position.y", elementName]];
  36. [self animationWithKeyPath:@"opacity" from:@([element.alpha.from getWithDefaultValue:1]) to:@([element.alpha.to getWithDefaultValue:1]) duration:[element.alpha.duration getWithDefaultValue:1] forView:view animationName:[NSString stringWithFormat:@"%@.alpha", elementName]];
  37. }
  38. @end