react-native-navigation的迁移库

RNNPushAnimation.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  13. UIView* fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
  14. UIView* toView = [transitionContext viewForKey:UITransitionContextToViewKey];
  15. [[transitionContext containerView] addSubview:fromView];
  16. [[transitionContext containerView] addSubview:toView];
  17. [CATransaction begin];
  18. [CATransaction setCompletionBlock:^{
  19. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  20. }];
  21. [self animateElement:self.screenTransition.topBar view:toViewController.navigationController.navigationBar elementName:@"topBar"];
  22. [self animateElement:self.screenTransition.content view:toViewController.view elementName:@"content"];
  23. [self animateElement:self.screenTransition.bottomTabs view:toViewController.tabBarController.tabBar elementName:@"bottomTabs"];
  24. [CATransaction commit];
  25. }
  26. - (void)animationWithKeyPath:(NSString *)keyPath from:(id)from to:(id)to duration:(CFTimeInterval)duration forView:(UIView *)view animationName:(NSString *)animationName {
  27. CABasicAnimation *animation = [CABasicAnimation animation];
  28. animation.keyPath = keyPath;
  29. animation.fromValue = from;
  30. animation.toValue = to;
  31. animation.duration = duration / 1000;
  32. [view.layer addAnimation:animation forKey:animationName];
  33. }
  34. - (void)animateElement:(ElementTransitionOptions *)element view:(UIView *)view elementName:(NSString *)elementName {
  35. [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"];
  36. [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]];
  37. [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]];
  38. }
  39. @end