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层级切换,同时会有从左向右推出的效果。
 
              
          

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import UIKit
 
class ViewController: UIViewController {
     
    override func viewDidLoad() {
        super.viewDidLoad()
         
        var redView:UIView = UIView(frame: CGRectMake(20,20,280,400))
        redView.backgroundColor = UIColor.redColor()
        self.view.insertSubview(redView, atIndex: 0)
         
        var blueView:UIView = UIView(frame: CGRectMake(20,20,280,400))
        blueView.backgroundColor = UIColor.blueColor()
        self.view.insertSubview(blueView, atIndex: 1)
    }
     
    //点击切换两个红蓝视图
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        var 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()
    }
}
posted @ 2015-09-28 10:19  brave-sailor  阅读(928)  评论(0编辑  收藏  举报