『ios』转场动画篇一 push pop转场实践

1536737739532.jpg

QQ20180912-163517-HD.gif
[viewB convertRect:viewC.frame toView:viewA];
viewA是目标,viewC是被操作的对象,那么剩下的viewB自然而然就是源了。作用就是计算viewB上的viewC相对于viewA的frame。
[viewC convertRect:viewB.frame fromView:viewA];
该例子viewA是源,viewB是被操作的对象,那么viewC就是目标。作用就是计算viewA上的viewB相对于viewC的frame。

先从文档来

UIViewControllerContextTransitioning
UIViewControllerAnimatedTransitioning

自定义简单转场动画上面两个protocol结合下面这两个方法就可以搞.

- (NSTimeInterval)transitionDuration:(nullable id )transitionContext;
// This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id )transitionContext;

还需要注意navigation的代理

UINavigationControllerDelegate

下面这个代理方法返回的是 就是上面UIViewControllerAnimatedTransitioning.

- (nullable id )navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);

为了完成动画还需要了解一个函数

- (nullable UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);

这个函数是获取view或者什么什么控件上的,截屏。afterUpdates是否全显示之后再截屏。

下面要开始了。
从第一步开始。
遵守代理

UINavigationControllerDelegate
 self.navigationController.delegate = self;
#pragma mark -- UINavigationControllerDelegate

- (id)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    
    if (operation == UINavigationControllerOperationPush) {
        return [customNavgationPushTransition new];//如果是push的话用我们定义的那个对象push对象
    } else if (operation == UINavigationControllerOperationPop && fromVC != self) {
        return [customNavgationPopTransition new];![QQ20180912-163517-HD.gif](https://upload-images.jianshu.io/upload_images/2440780-5e5411e05137385e.gif?imageMogr2/auto-orient/strip)

    } else {
        return nil;
    }
}

如果没有这一步,自定义的专场动画也就不会生效。因为没有返回UIViewControllerAnimatedTransitioning。

第二步,自定义push动画

//动画时长
- (NSTimeInterval)transitionDuration:(nullable id )transitionContext {
    return 0.5;
}

//
- (void)animateTransition:(id )transitionContext {
    
    //1.获取动画的源控制器和目标控制器
    ViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    DetailViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *container = [transitionContext containerView];
    
    //2.创建一个   imageView 的截图,并把 imageView 隐藏,造成使用户以为移动的就是 imageView 的假象
    UIView *snapshotView = [fromVC.ImageView snapshotViewAfterScreenUpdates:NO];
    //计算fromVC.view上的fromVC.ImageView.frame相对于container的坐标
    snapshotView.frame = [container convertRect:fromVC.ImageView.frame fromView:fromVC.view];
//    fromVC.selectedCell.imageView.hidden = YES;
    
    //3.设置目标控制器的位置,并把透明度设为0,在后面的动画中慢慢显示出来变为1
    toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
    toVC.view.alpha = 0;
    toVC.bgImageView.hidden = YES;
    
    //4.都添加到 container 中。注意顺序不能错了
    [container addSubview:toVC.view];
    [container addSubview:snapshotView];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        snapshotView.frame = [container convertRect:toVC.bgImageView.frame fromView:toVC.view];
        fromVC.view.alpha = 0;
        toVC.view.alpha = 1;
    } completion:^(BOOL finished) {
        fromVC.ImageView.hidden = NO;
        toVC.bgImageView.hidden = NO;
        [snapshotView removeFromSuperview];
        
        //一定要记得动画完成后执行此方法,让系统管理 navigation 如果设置为no。可以自己试试
        [transitionContext completeTransition:YES];
    }];
    
}

完成这一步那么push的时候就会有动画了。

第三步设置pop动画,跟push大同小异。

//动画时长
- (NSTimeInterval)transitionDuration:(nullable id )transitionContext {
    return 0.5;
}

//
- (void)animateTransition:(id )transitionContext {
    
    //1.获取动画的源控制器和目标控制器
    DetailViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    ViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *container = [transitionContext containerView];
    
    //2.创建一个 Cell 中 imageView 的截图,并把 imageView 隐藏,造成使用户以为移动的就是 imageView 的假象
    UIView *snapshotView = [fromVC.bgImageView snapshotViewAfterScreenUpdates:NO];
    snapshotView.frame = [container convertRect:fromVC.bgImageView.frame fromView:fromVC.view];
    fromVC.bgImageView.hidden = YES;
    
    //3.设置目标控制器的位置,并把透明度设为0,在后面的动画中慢慢显示出来变为1
    toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
    toVC.view.alpha = 0;
//    toVC..hidden = YES;
    
    //4.都添加到 container 中。注意顺序不能错了
    [container addSubview:toVC.view];
    [container addSubview:snapshotView];
    
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        snapshotView.frame = [container convertRect:toVC.ImageView.frame fromView:toVC.view];
        fromVC.view.alpha = 0;
        toVC.view.alpha = 1;
    } completion:^(BOOL finished) {
        fromVC.bgImageView.hidden = NO;
//        toVC.selectedCell.hidden = NO;
        [snapshotView removeFromSuperview];
        
        //一定要记得动画完成后执行此方法,让系统管理 navigation
        [transitionContext completeTransition:YES];
    }];
    
}

demo地址(https://github.com/Butteryflyyer/XH_CustomTransitionl)

ios自习室欢迎进入,一起学习一起进步。

『ios』转场动画篇一 push pop转场实践_第1张图片
IMG_7291.JPG

你可能感兴趣的:(『ios』转场动画篇一 push pop转场实践)