12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //
- // 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<UIViewControllerContextTransitioning>)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<UIViewControllerContextTransitioning>)transitionContext {
- return 0.25;
- }
- @end
|