// // NavigationAnimator.m // XenonSDK // // Created by SAGESSE on 2019/5/29. // Copyright © 2019 SAGESSE. All rights reserved. // #import "NavigationAnimator.h" #import "BaseViewController.h" @interface NavigationAnimator () @property UINavigationControllerOperation operation; @property UILabel* titleView; @end @implementation NavigationAnimator - (instancetype)initWithOperation:(UINavigationControllerOperation)operation titleView:(UILabel*)titleView { self = [super init]; self.operation = operation; self.titleView = titleView; return self; } - (void)animateTransition:(nonnull id)transitionContext { UIView* fromView = [transitionContext viewForKey:UITransitionContextFromViewKey]; UIView* toView = [transitionContext viewForKey:UITransitionContextToViewKey]; if (self.titleView != nil) { UIViewController* viewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; if ([viewController isKindOfClass:BaseViewController.self]) { self.titleView.text = [(BaseViewController*)viewController topTitle]; } } switch (self.operation) { case UINavigationControllerOperationPush: { toView.frame = fromView.frame; toView.transform = CGAffineTransformMakeTranslation(toView.frame.size.width, 0); fromView.transform = CGAffineTransformIdentity; [transitionContext.containerView addSubview:toView]; [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:100 initialSpringVelocity:0 options:0 animations:^{ fromView.transform = CGAffineTransformMakeTranslation(-toView.frame.size.width, 0); toView.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { fromView.transform = CGAffineTransformIdentity; [fromView removeFromSuperview]; [transitionContext completeTransition:YES]; }]; break; } case UINavigationControllerOperationPop: { toView.frame = fromView.frame; toView.transform = CGAffineTransformMakeTranslation(-toView.frame.size.width, 0); fromView.transform = CGAffineTransformIdentity; [transitionContext.containerView addSubview:toView]; [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:100 initialSpringVelocity:0 options:0 animations:^{ fromView.transform = CGAffineTransformMakeTranslation(toView.frame.size.width, 0); toView.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { fromView.transform = CGAffineTransformIdentity; [fromView removeFromSuperview]; [transitionContext completeTransition:YES]; }]; break; } default: break; } } - (NSTimeInterval)transitionDuration:(nullable id)transitionContext { return 0.25; } @end