參考:第五篇:CABasic Animation溃槐。iOS動(dòng)畫系列之五:基礎(chǔ)動(dòng)畫之縮放篇&旋轉(zhuǎn)篇
原理和上一篇是相同的蜘澜,不同之處是keyPath
屬性惧蛹,縮放時(shí)是keyPath
是 transform.scale右冻,旋轉(zhuǎn)時(shí)keyPath
是transform.rotation档玻。
class ScaleRotationViewController: UIViewController {
// 用于縮放的layer
lazy var redLayer: CALayer = {
return self.createLayer(position: CGPoint(x: 125, y: 150), backgroundColor: UIColor.red)
}()
lazy var greenLayer: CALayer = {
return self.createLayer(position: CGPoint(x: 125, y: 250), backgroundColor: UIColor.green)
}()
lazy var cyanLayer: CALayer = {
return self.createLayer(position: CGPoint(x: 125, y: 350), backgroundColor: UIColor.cyan)
}()
lazy var blueLayer: CALayer = {
return self.createLayer(position: CGPoint(x: 125, y: 450), backgroundColor: UIColor.blue)
}()
// 用于旋轉(zhuǎn)的Layer
lazy var redRotationLayer: CALayer = {
return self.createLayer(position: CGPoint(x: 300, y: 150), backgroundColor: UIColor.red)
}()
lazy var greenRotationLayer: CALayer = {
return self.createLayer(position: CGPoint(x: 300, y: 250), backgroundColor: UIColor.green)
}()
lazy var cyanRotationLayer: CALayer = {
return self.createLayer(position: CGPoint(x: 300, y: 350), backgroundColor: UIColor.cyan)
}()
override func viewDidLoad() {
super.viewDidLoad()
// 添加縮放的動(dòng)畫
redLayer.add(createAnimation(keyPath: "transform.scale.x", toValue: 0.5), forKey: nil)
greenLayer.add(createAnimation(keyPath: "transform.scale.y", toValue: 0.5), forKey: nil)
cyanLayer.add(createAnimation(keyPath: "transform.scale.z", toValue: 0.5), forKey: nil)
blueLayer.add(createAnimation(keyPath: "transform.scale", toValue: 0.5), forKey: nil)
// 添加旋轉(zhuǎn)的動(dòng)畫
redRotationLayer.add(createAnimation(keyPath: "transform.rotation.x", toValue: CGFloat(Double.pi / 4)), forKey: nil)
greenRotationLayer.add(createAnimation(keyPath: "transform.rotation.y", toValue: CGFloat(Double.pi / 4)), forKey: nil)
cyanRotationLayer.add(createAnimation(keyPath: "transform.rotation.z", toValue: CGFloat(Double.pi / 4)), forKey: nil)
self.creatLabel(title: "縮放", frame: CGRect(x: 100, y: 80, width: 50, height: 20))
self.creatLabel(title: "旋轉(zhuǎn)", frame: CGRect(x: 250, y: 80, width: 50, height: 20))
self.creatLabel(title: "X軸", frame: CGRect(x: 20, y: 125, width: 50, height: 20))
self.creatLabel(title: "Y軸", frame: CGRect(x: 20, y: 225, width: 50, height: 20))
self.creatLabel(title: "Z軸", frame: CGRect(x: 20, y: 325, width: 50, height: 20))
self.creatLabel(title: "XYZ軸", frame: CGRect(x: 20, y: 425, width: 50, height: 20))
}
// 創(chuàng)建calayer
func createLayer (position: CGPoint, backgroundColor: UIColor) -> CALayer {
//創(chuàng)建calayer
let layer = CALayer()
//設(shè)置位置和大小
layer.position = position
layer.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
//設(shè)置背景顏色
layer.backgroundColor = backgroundColor.cgColor
//把layer添加到UIView的layer上
self.view.layer.addSublayer(layer)
return layer
}
// 創(chuàng)建基礎(chǔ)Animation
func createAnimation (keyPath: String, toValue: CGFloat) -> CABasicAnimation {
//創(chuàng)建動(dòng)畫對(duì)象
let scaleAni = CABasicAnimation()
//設(shè)置動(dòng)畫屬性
scaleAni.keyPath = keyPath
//設(shè)置動(dòng)畫的起始位置。也就是動(dòng)畫從哪里到哪里躯畴。不指定起點(diǎn)民鼓,默認(rèn)就從positoin開始
scaleAni.toValue = toValue
//動(dòng)畫持續(xù)時(shí)間
scaleAni.duration = 2;
//動(dòng)畫重復(fù)次數(shù)
scaleAni.repeatCount = Float(CGFloat.greatestFiniteMagnitude)
return scaleAni;
}
//創(chuàng)建Label
fileprivate func creatLabel (title: String, frame: CGRect) -> (){
let label = UILabel()
label.text = title
label.frame = frame
label.textColor = UIColor.darkGray
label.font = UIFont.systemFont(ofSize: 12)
self.view.addSubview(label)
}
}