美文网首页ios 知识点iOS开发iOS分享的demo
自定义present和dismiss的转场动画

自定义present和dismiss的转场动画

作者: 跑调的安眠曲 | 来源:发表于2016-08-13 16:40 被阅读725次

    趁周末闲暇之余,重新写一下present和push的自定义动画。
    本来之前有写过一个,因为没有及时整理,到导致一时凌乱,找不到具体在哪了,提醒各位,平时要注意代码的整理和归档,不然到时候重复的代码写了又写,那就得不偿失了。废话不过说,先看下下面的效果:

    效果图展示

    原生的present是从底部向上弹出相应的视图控制器,而push动画,则是将视图从右边推出来进行展示,为了视图展示的统一性,需要自定义转场动画,将present改成从右往左推出来展示,在dismiss的VC中需要增加一个左边的手势,以满足侧滑返回的效果。

    首先需要主页VC实现UIViewControllerTransitioningDelegate这个代理中的2个方法

    #pragma mark - UIViewControllerTransitioningDelegate
    - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
        return [[YQPresentTransitionAnimated alloc] init];
    }
    
    - (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
        return [[YQDismissTransitionAnimated alloc] init];
    }
    

    将它的present动画和dismiss动画交给我们自定义的动画来管理。

    present动画的自定义####

    1.创建一个类,继承至NSObject,并实现UIViewControllerAnimatedTransitioning的代理;

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    @interface YQPresentTransitionAnimated : NSObject<UIViewControllerAnimatedTransitioning>
    
    @end
    

    2.实现代理的2个方法

    //1.定义转场动画的时间
    - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
        return 0.4;
    }
    
    //2.实现转场动画的动画效果
    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
        UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];   //主页VC
        UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];     //present的VC
        
        UIView *containerView = transitionContext.containerView;  //转场的容器视图,动画完成后,会消失
        UIView *fromView;
        UIView *toView;
    
        if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
            fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
            toView = [transitionContext viewForKey:UITransitionContextToViewKey];
        } else {
            fromView = fromViewController.view;
            toView = toViewController.view;
        }
        
        //注意这个对应关系
        BOOL isPresenting = (toViewController.presentingViewController == fromViewController);
        
        CGRect fromFrame = [transitionContext initialFrameForViewController:fromViewController];
        CGRect toFrame = [transitionContext finalFrameForViewController:toViewController];
        
        if (isPresenting) {
            fromView.frame = fromFrame;
            toView.frame = CGRectOffset(toFrame, toFrame.size.width, 0);
        }
        
        if (isPresenting)
            [containerView addSubview:toView];
        
        NSTimeInterval transitionDuration = [self transitionDuration:transitionContext];
        
        [UIView animateWithDuration:transitionDuration animations:^{
            if (isPresenting) {
                toView.frame = toFrame;
                fromView.frame = CGRectOffset(fromFrame, fromFrame.size.width * 0.3 * -1, 0);
            }
        } completion:^(BOOL finished) {
            //固定写法
            BOOL wasCancelled = [transitionContext transitionWasCancelled];
            
            if (wasCancelled)
                [toView removeFromSuperview];
            
            [transitionContext completeTransition:!wasCancelled];
        }];
    }
    

    3.将主页VC的transitioningDelegate设为自己,【千万注意】:还需要将present的页面的transitioningDelegate设为自己(主页VC)

    dismiss动画的自定义####

    跟上面的present步骤一样,只需将动画效果换成相反的即可。

    附上源码:
    https://github.com/GitterYang/YQPresentAnimation

    相关文章

      网友评论

      • LD_左岸:我自定义的present和dismiss 但是在真机上 dismiss时 首页会闪一下
        LD_左岸:@跑调的安眠曲 我自定义的 解决了 是因为画圆圈的时候 起始点没搞对
        跑调的安眠曲:是我的例子?还是你自定义的?
      • 迟明子:已经用了你的 从被modal出来的ViewController,在没有导航栏的情况下也可以手势右滑返回,我就服你!!!给了你一个Star!!!感谢!!!

      本文标题:自定义present和dismiss的转场动画

      本文链接:https://www.haomeiwen.com/subject/xnzesttx.html