12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #import "RNNAnimationsTransitionDelegate.h"
-
- @implementation RNNAnimationsTransitionDelegate
-
- - (instancetype)initWithScreenTransition:(TransitionOptions *)transitionOptions isDismiss:(BOOL)isDismiss {
- self = [super init];
- self.transitionOptions = transitionOptions;
- self.isDismiss = isDismiss;
- return self;
- }
-
- - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
- return self;
- }
-
- - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
- return self;
- }
-
- - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
- return self.transitionOptions.maxDuration;
- }
-
- - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
- UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
- UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
-
- [CATransaction begin];
- [CATransaction setCompletionBlock:^{
- [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
- }];
-
- if (_isDismiss) {
- [[transitionContext containerView] addSubview:toViewController.view];
- [[transitionContext containerView] addSubview:fromViewController.view];
- [self animateElement:self.transitionOptions view:fromViewController.view elementName:@"content"];
- } else {
- [[transitionContext containerView] addSubview:toViewController.view];
- [self animateElement:self.transitionOptions view:toViewController.view elementName:@"content"];
- }
-
- [CATransaction commit];
- }
-
- - (void)animationWithKeyPath:(NSString *)keyPath from:(id)from to:(id)to duration:(CFTimeInterval)duration forView:(UIView *)view animationName:(NSString *)animationName {
- CABasicAnimation *animation = [CABasicAnimation animation];
- animation.keyPath = keyPath;
- animation.fromValue = from;
- animation.toValue = to;
- animation.duration = duration / 1000;
- [view.layer addAnimation:animation forKey:animationName];
- }
-
- - (void)animateElement:(TransitionOptions *)transition view:(UIView *)view elementName:(NSString *)elementName {
- [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"];
- [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]];
- [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]];
- }
-
- @end
|