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

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

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

目录

CATransition

CATransition的type属性:


kCATransitionFade    //淡入淡出(默认)

kCATransitionMoveIn  //移入

kCATransitionPush    //压入

kCATransitionReveal  //渐变

CATransition的subtype属性:


kCATransitionFromRight

kCATransitionFromLeft

kCATransitionFromTop

kCATransitionFromBottom

代码示例:

    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的类型:

//水平翻转:
FlipFromLeft
FlipFromRight

//卷页效果:
CurlUp
CurlDown

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()
    }
上一篇:[快速学会Swift第三方库] SQLite.swift篇


下一篇:快速排序 python