• 自定义模态转场动画


    1.自定义模态视图切换动画

    新建一个类实现UIviewControllerAnimatedTransitioning协议  这个类就是我们要用到的自定义的动画切换类

    #import "ModelTransitionAnimation.h"

    //需要模态出来的控制器

    #import "ModelViewController.h"

    @implementation ModelTransitionAnimation

    //动画持续时间,单位是秒

    - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext

    {

        return 1;

    }

    //动画效果

    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext

    {

        //通过键值UITranstionContexToViewControllerKey 获取需要呈现的视图控制器VC

        UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

        //得到toVc完全呈现后的frame

        CGRect finalFrame = [transitionContext finalFrameForViewController:toVc];

        if ([toVc isKindOfClass:[ModelViewController class]]) {

            //需要呈现的视图是模态视图,此时将模态视图的frame放到屏幕下方,这样才能实现从下方弹出的效果

            toVc.view.frame = CGRectOffset(finalFrame, 0, [UIScreen mainScreen].bounds.size.height);

        }else

        {

            //需要呈现的视图是主视图,此时将主视图的frame 放到屏幕空间上方 这样才能实现从上方放下的效果

            toVc.view.frame = CGRectOffset(finalFrame, 0, -[UIScreen mainScreen].bounds.size.height);

        }

        //切换在containerView中完成,需要将toVc.view加到containerView

        UIView *containerView = [transitionContext containerView];

        [containerView addSubview:toVc.view];

        //开始动画  这里使用UIKit提供的弹簧效果动画

        //useingSpringWithDamping越接近1 弹性效果越不明显

        [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{

            toVc.view.frame = finalFrame;

        } completion:^(BOOL finished) {

            //通知系统动画切换完成

            [transitionContext completeTransition:YES];

        }];

     

    }

    控制器需要做的事情就是将系统的自带的动画替换掉

    需遵守ModelViewControllerDelegate,UIViewControllerTransitioningDelegate 这两个协议 

     

     

    - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source

     

    {

     

        return _animation;

     

    }

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    StringUtils工具类的使用
    struts2 文件上传和下载,以及部分源代码解析
    ios开发之猜数字游戏
    从epoll构建muduo-12 多线程入场
    POJ3009 Curling 2.0(DFS)
    IOS-4-面试题1:黑马程序猿IOS面试题大全
    Android-Universal-Image-Loader载入图片
    《UNIX环境高级编程》读书笔记 —— 文件 I/O
    畅通project再续 HDU杭电1875 【Kruscal算法 || Prim】
    轻松学习之Linux教程四 神器vi程序编辑器攻略
  • 原文地址:https://www.cnblogs.com/zmloveworld/p/6429032.html
Copyright © 2020-2023  润新知