Swift - CATransition制作过渡动画(页面切换转场效果)

CATransition动画主要在过渡时使用,比如两个页面层级改变的时候添加一个转场效果。CATransition分为两类,一类是公开的动画效果,一类是非公开的动画效果。

1,公开动画效果:
kCATransitionFade:翻页 kCATransitionMoveIn:弹出 kCATransitionPush:推出 kCATransitionReveal:移除

2,非公开动画效果:
"cube":立方体 "suckEffect":吸收 "oglFlip":翻转 "rippleEffect":波纹 "pageCurl":卷页 "cameraIrisHollowOpen":镜头开 "cameraIrisHollowClose":镜头关

3,动画方向类型:
kCATransitionFromRight:从右侧开始实现过渡动画 kCATransitionFromLeft:从左侧开始实现过渡动画 kCATransitionFromTop:从顶部开始实现过渡动画 kCATransitionFromBottom:从底部开始实现过渡动画

4,下面通过一个样例演示:
页面上添加两个分别是红色,蓝色的UIView。当点击屏幕的时候,这两个UIView层级切换,同时会有从左向右推出的效果。


Swift - CATransition制作过渡动画(页面切换转场效果)_第1张图片
import UIKit
 
class ViewController: UIViewController {
     
    override func viewDidLoad() {
        super.viewDidLoad()
         
        let redView:UIView = UIView(frame: CGRectMake(20,20,280,400))
        redView.backgroundColor = UIColor.redColor()
        self.view.insertSubview(redView, atIndex: 0)
         
        let blueView:UIView = UIView(frame: CGRectMake(20,20,280,400))
        blueView.backgroundColor = UIColor.blueColor()
        self.view.insertSubview(blueView, atIndex: 1)
    }
     
    //点击切换两个红蓝视图
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
        let transition = CATransition()
        transition.duration = 3.0
        transition.type = kCATransitionPush //推送类型
        transition.subtype = kCATransitionFromLeft //从左侧
        self.view.exchangeSubviewAtIndex(1, withSubviewAtIndex: 0)
        self.view.layer.addAnimation(transition, forKey: nil)
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

你可能感兴趣的:(Swift - CATransition制作过渡动画(页面切换转场效果))