人一切的痛苦,本质上都是对自己的无能的愤怒。
** 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动画也是一样的做法,只是看你要的动画效果同来改第二个方法
网友评论