Swift学习笔记(1)过渡动画(CATransition和UIViewAnimation)的用法

来源:互联网 发布:初学者的编程软件mac 编辑:程序博客网 时间:2024/05/22 00:22

Swift学习笔记(1)过渡动画(CATransition和UIViewAnimation)的用法

CATransition和UIViewAnimation是场景切换时常用的两种过渡动画

目录

  • Swift学习笔记1过渡动画CATransition和UIViewAnimation的用法
    • 目录
    • CATransition
      • CATransition的type属性
      • CATransition的subtype属性
      • 代码示例
    • UIViewAnimationTransition
      • UIViewAnimationTransition的类型
      • UIViewAnimationTransition的类型
      • 代码示例

CATransition

CATransition的type属性:

kCATransitionFade    //淡入淡出(默认)kCATransitionMoveIn  //移入kCATransitionPush    //压入kCATransitionReveal  //渐变

CATransition的subtype属性:

kCATransitionFromRightkCATransitionFromLeftkCATransitionFromTopkCATransitionFromBottom

代码示例:

    func change() {        // 初始化动画的持续时间,类型和子类型        let transition = CATransition()        transition.duration = 2.0        transition.type = kCATransitionReveal        transition.subtype = kCATransitionFromLeft        let nextView = self.storyboard?.instantiateViewControllerWithIdentifier("next") as! NextViewController        self.view.addSubview(nextView.view)        // 执行刚才添加好的动画        self.view.layer.addAnimation(transition, forKey: nil)    }

UIViewAnimationTransition

UIViewAnimationTransition的类型:

//水平翻转:FlipFromLeftFlipFromRight//卷页效果:CurlUpCurlDown

UIViewAnimationTransition的类型:

EaseInOut  //动画由慢变快再变慢EaseIn     //动画由慢变快EaseOut    //动画由快变慢Linear     //匀速动画

代码示例:

    func change() {        let nextView = self.storyboard?.instantiateViewControllerWithIdentifier("next") as! ViewController        self.view.addSubview(nextView.view)        UIView.beginAnimations("", context: nil)        //设置动画的持续时间,类型和渐变类型        UIView.setAnimationDuration(0.5)        UIView.setAnimationTransition(UIViewAnimationTransition.CurlDown, forView: self.view, cache: true)        UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut)        //开始动画        UIView.commitAnimations()    }
1 0