人一切的痛苦,本質(zhì)上都是對(duì)自己的無(wú)能的憤怒显歧。
** UINavigationController切換是有一個(gè)默認(rèn)動(dòng)畫(huà)的蔗牡,但這個(gè)默認(rèn)動(dòng)畫(huà),在有些時(shí)候就顯得不是很適用愚臀,比如查看照片詳情的時(shí)候忆蚀,從右邊側(cè)滑過(guò)來(lái)就顯得比較蠢了,所以需要我們自定義一下,如從照片中生出一個(gè)照片來(lái)顯示**
目錄
UINavigationControllerDelegate(準(zhǔn)守協(xié)議馋袜,讓系統(tǒng)運(yùn)行我們自定義的動(dòng)畫(huà))
UIViewControllerAnimatedTransitioning(動(dòng)畫(huà)怎么運(yùn)行)
UINavigationControllerDelegate
這里就是在你的ViewController里面準(zhǔn)守協(xié)議男旗,然后實(shí)現(xiàn)方法,讓系統(tǒng)選擇我們自定義的動(dòng)畫(huà)來(lái)代替系統(tǒng)動(dòng)畫(huà)
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
/*注:這里的 PushTransition 就是我自定義的push動(dòng)畫(huà)桃焕,
是準(zhǔn)守 UIViewControllerAnimatedTransitioning 協(xié)議的 NSObject*/
if (operation == UINavigationControllerOperationPush) {
//返回我們自定義的效果
PushTransition * pushT = [[PushTransition alloc]init];
pushT.point = self.point;
pushT.image = self.selectImage;
// return [[PushTransition alloc]init];
return pushT;
}
else if (operation == UINavigationControllerOperationPop){
return [[PopTransition alloc]init];
}
//返回nil則使用默認(rèn)的動(dòng)畫(huà)效果
return nil;
}
//在這里設(shè)置代理
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.delegate = self;
}
UIViewControllerAnimatedTransitioning
這里就是你自定義動(dòng)畫(huà)怎么運(yùn)行的剑肯,每次切換需要多長(zhǎng)時(shí)間等設(shè)置
//返回動(dòng)畫(huà)執(zhí)行時(shí)間捧毛,單位是秒
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 1.0f;
}
//在這里是決定動(dòng)畫(huà)是怎么執(zhí)行的
/**
* 這個(gè)動(dòng)畫(huà)實(shí)際做法就是將接下來(lái)要顯示的頁(yè)面先隱藏在右邊 然后在 當(dāng)前頁(yè)面上添加一個(gè)ImageView
* 用這個(gè)ImageView來(lái)實(shí)現(xiàn)動(dòng)畫(huà) 最后將這個(gè)ImageView移除 同時(shí)將 to 放到應(yīng)該有的位置
*/
/**
*動(dòng)畫(huà)效果就是開(kāi)頭效果圖中的點(diǎn)擊cell的效果
*/
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
self.transitionContext = transitionContext;
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//不添加的話观堂,屏幕什么都沒(méi)有
UIView *containerView = [transitionContext containerView];
[containerView addSubview:fromVC.view];
[containerView addSubview:toVC.view];
float width = fromVC.view.frame.size.width;
//將toview放到旁邊
toVC.view.frame = CGRectMake(width, 0, width, fromVC.view.frame.size.height);
CGRect finalFrame = [transitionContext finalFrameForViewController:toVC];
NSTimeInterval animateTime = [self transitionDuration:transitionContext];
UIImageView * imageView = [[UIImageView alloc]initWithImage:self.image];
imageView.frame = (CGRect){self.point.x,self.point.y,CGSizeZero};
[fromVC.view addSubview:imageView];
[UIView animateWithDuration:animateTime animations:^{
imageView.frame = finalFrame;
} completion:^(BOOL finished) {
toVC.view.frame = finalFrame;
[imageView removeFromSuperview];
//結(jié)束動(dòng)畫(huà)
[self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
}];
}
上面這個(gè)是push動(dòng)畫(huà),pop動(dòng)畫(huà)也是一樣的做法呀忧,只是看你要的動(dòng)畫(huà)效果同來(lái)改第二個(gè)方法