1:第一步:編寫(xiě)一個(gè)繼承NSObject的動(dòng)畫(huà)類(lèi)YLAnimator遵循協(xié)議UIViewControllerAnimatedTransitioning
實(shí)現(xiàn)委托方法:
- (NSTimeInterval)transitionDuration:(id)transitionContext{ //動(dòng)畫(huà)時(shí)長(zhǎng)
return 1;
}
- (void)animateTransition:(id)transitionContext ? {//動(dòng)畫(huà)實(shí)現(xiàn)
? ? ? UIViewController* toViewController = [transitionContext ? ?viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
CGAffineTransform transform = CGAffineTransformIdentity愚隧;
//transform的具體實(shí)現(xiàn)形式
//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類(lèi) YLNavigationDelegate
實(shí)現(xiàn)委托方法UINavigationControllerDelegate
- (id)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
? ? if (operation == UINavigationControllerOperationPush ) ? ?{
? ? ? ? ? ?return self.animator; ?//自定義的動(dòng)畫(huà)
? ? ?}
}
return nil;
}
3:第三步录煤,修改自定義的YLNavigationController,一般繼承自UINavigationController
-(instancetype)initWithRootViewController:(UIViewController *)rootViewController{
? ? ? if(self = [super initWithRootViewController:rootViewController]){
? ? ? ? ? _navDelegate = [[YLNavigationControllerDelegate alloc] init]; ??
? ? ? ? self.delegate = _navDelegate;
}
return self
}