人一切的痛苦,本質上都是對自己的無能的憤怒。
** UINavigationController切換是有一個默認動畫的,但這個默認動畫,在有些時候就顯得不是很適用,比如查看照片詳情的時候,從右邊側滑過來就顯得比較蠢了,所以需要我們自定義一下,如從照片中生出一個照片來顯示**
效果圖
目錄
UINavigationControllerDelegate(準守協議,讓系統運行我們自定義的動畫)
UIViewControllerAnimatedTransitioning(動畫怎么運行)
UINavigationControllerDelegate
這里就是在你的ViewController里面準守協議,然后實現方法,讓系統選擇我們自定義的動畫來代替系統動畫
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
/*注:這里的 PushTransition 就是我自定義的push動畫,
是準守 UIViewControllerAnimatedTransitioning 協議的 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則使用默認的動畫效果
return nil;
}
//在這里設置代理
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.delegate = self;
}
UIViewControllerAnimatedTransitioning
這里就是你自定義動畫怎么運行的,每次切換需要多長時間等設置
//返回動畫執行時間,單位是秒
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 1.0f;
}
//在這里是決定動畫是怎么執行的
/**
* 這個動畫實際做法就是將接下來要顯示的頁面先隱藏在右邊 然后在 當前頁面上添加一個ImageView
* 用這個ImageView來實現動畫 最后將這個ImageView移除 同時將 to 放到應該有的位置
*/
/**
*動畫效果就是開頭效果圖中的點擊cell的效果
*/
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
self.transitionContext = transitionContext;
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//不添加的話,屏幕什么都沒有
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];
//結束動畫
[self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
}];
}
上面這個是push動畫,pop動畫也是一樣的做法,只是看你要的動畫效果同來改第二個方法