CATransition 动画主要在过渡时使用,比如两个页面层级改变的时候添加一个转场效果。CATransition 分为两类,一类是公开的动画效果,一类是非公开的动画效果。
1、公开动画效果(CATransitionType):
- .fade:翻页
- .moveIn:弹出
- .push:推出
- .reveal:移除
2、非公开动画效果:
- "cube":立方体
- "suckEffect":吸收
- "oglFlip":翻转
- "rippleEffect":波纹
- "pageCurl":卷页
- "cameraIrisHollowOpen":镜头开
- "cameraIrisHollowClose":镜头关
3、动画方向类型(CATransitionSubtype):
- .fromRight:从右侧开始实现过渡动画
- .fromLeft:从左侧开始实现过渡动画
- .fromTop:从顶部开始实现过渡动画
- .fromBottom:从底部开始实现过渡动画
4、下面通过一个样例演示:
页面上添加两个分别是红色,蓝色的 UIView。当点击屏幕的时候,这两个 UIView 层级切换,同时会有从左向右推出的效果。
代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let redView:UIView = UIView(frame: CGRect(x:20, y:20, width:280, height:400))
redView.backgroundColor = UIColor.red
self.view.insertSubview(redView, at: 0)
let blueView:UIView = UIView(frame: CGRect(x:20, y:20, width: 280, height:400))
blueView.backgroundColor = UIColor.blue
self.view.insertSubview(blueView, at: 1)
}
//点击切换两个红蓝视图
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let transition = CATransition()
transition.duration = 3.0
transition.type = kCATransitionPush //推送类型
transition.subtype = kCATransitionFromLeft //从左侧
self.view.exchangeSubview(at: 1, withSubviewAt: 0)
self.view.layer.add(transition, forKey: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
原文出自:www.hangge.com
网友评论