1:第一步:编写一个继承NSObject的动画类YLAnimator遵循协议UIViewControllerAnimatedTransitioning
实现委托方法:
- (NSTimeInterval)transitionDuration:(id)transitionContext{ //动画时长
return 1;
}
- (void)animateTransition:(id)transitionContext {//动画实现
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
CGAffineTransform transform = CGAffineTransformIdentity;
//transform的具体实现形式
//eg tansform = CGAffineTransformTranslate(tansform, <#CGFloat tx#>, <#CGFloat ty#>)
toViewController.view.transform = transform;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toViewController.view.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
fromViewController.view.transform = CGAffineTransformIdentity;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
2.第二步,自定义UINavigationDelegate类 YLNavigationDelegate
实现委托方法UINavigationControllerDelegate
- (id)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
if (operation == UINavigationControllerOperationPush ) {
return self.animator; //自定义的动画
}
}
return nil;
}
3:第三步,修改自定义的YLNavigationController,一般继承自UINavigationController
-(instancetype)initWithRootViewController:(UIViewController *)rootViewController{
if(self = [super initWithRootViewController:rootViewController]){
_navDelegate = [[YLNavigationControllerDelegate alloc] init];
self.delegate = _navDelegate;
}
return self
}