react-native-navigation的迁移库

RNNAnimationsTransitionDelegate.m 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #import "RNNAnimationsTransitionDelegate.h"
  2. @implementation RNNAnimationsTransitionDelegate
  3. - (instancetype)initWithScreenTransition:(RNNScreenTransition *)screenTransition isDismiss:(BOOL)isDismiss {
  4. self = [super init];
  5. self.screenTransition = screenTransition;
  6. self.isDismiss = isDismiss;
  7. return self;
  8. }
  9. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  10. return self;
  11. }
  12. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  13. return self;
  14. }
  15. - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
  16. return self.screenTransition.maxDuration / 1000;
  17. }
  18. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
  19. UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  20. UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  21. [CATransaction begin];
  22. [CATransaction setCompletionBlock:^{
  23. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  24. }];
  25. if (_isDismiss) {
  26. [[transitionContext containerView] addSubview:toViewController.view];
  27. [[transitionContext containerView] addSubview:fromViewController.view];
  28. [self animateElement:self.screenTransition.content view:fromViewController.view elementName:@"content"];
  29. } else {
  30. [[transitionContext containerView] addSubview:toViewController.view];
  31. [self animateElement:self.screenTransition.content view:toViewController.view elementName:@"content"];
  32. }
  33. [CATransaction commit];
  34. }
  35. - (void)animationWithKeyPath:(NSString *)keyPath from:(id)from to:(id)to duration:(CFTimeInterval)duration forView:(UIView *)view animationName:(NSString *)animationName {
  36. CABasicAnimation *animation = [CABasicAnimation animation];
  37. animation.keyPath = keyPath;
  38. animation.fromValue = from;
  39. animation.toValue = to;
  40. animation.duration = duration / 1000;
  41. [view.layer addAnimation:animation forKey:animationName];
  42. }
  43. - (void)animateElement:(RNNElementTransitionOptions *)element view:(UIView *)view elementName:(NSString *)elementName {
  44. [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"];
  45. [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]];
  46. [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]];
  47. }
  48. @end