react-native-navigation的迁移库

RNNAnimationsTransitionDelegate.m 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #import "RNNAnimationsTransitionDelegate.h"
  2. @implementation RNNAnimationsTransitionDelegate
  3. - (instancetype)initWithScreenTransition:(TransitionOptions *)transitionOptions isDismiss:(BOOL)isDismiss {
  4. self = [super init];
  5. self.transitionOptions = transitionOptions;
  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.transitionOptions.maxDuration;
  17. }
  18. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
  19. UIView* toView = [transitionContext viewForKey:UITransitionContextToViewKey];
  20. UIView* fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
  21. [CATransaction begin];
  22. [CATransaction setCompletionBlock:^{
  23. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  24. }];
  25. if (_isDismiss) {
  26. [[transitionContext containerView] addSubview:toView];
  27. [[transitionContext containerView] addSubview:fromView];
  28. [self animateElement:self.transitionOptions view:fromView elementName:@"content"];
  29. } else {
  30. [[transitionContext containerView] addSubview:toView];
  31. [self animateElement:self.transitionOptions view:toView 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:(TransitionOptions *)transition view:(UIView *)view elementName:(NSString *)elementName {
  44. [self animationWithKeyPath:@"position.x" from:@(view.layer.position.x + [transition.x.from getWithDefaultValue:0]) to:@(view.layer.position.x + [transition.x.to getWithDefaultValue:0]) duration:[transition.x.duration getWithDefaultValue:1] forView:view animationName:@"transition.position.x"];
  45. [self animationWithKeyPath:@"position.y" from:@(view.layer.position.y + [transition.y.from getWithDefaultValue:0]) to:@(view.layer.position.y + [transition.y.to getWithDefaultValue:0]) duration:[transition.y.duration getWithDefaultValue:1] forView:view animationName:[NSString stringWithFormat:@"%@.position.y", elementName]];
  46. [self animationWithKeyPath:@"opacity" from:@([transition.alpha.from getWithDefaultValue:1]) to:@([transition.alpha.to getWithDefaultValue:1]) duration:[transition.alpha.duration getWithDefaultValue:1] forView:view animationName:[NSString stringWithFormat:@"%@.alpha", elementName]];
  47. }
  48. @end