核心動畫(Core Animation)
CAAnimation繼承結(jié)構(gòu)
注意:上圖中的黑色虛線代表”繼承”某個類哪工,紅色虛線代表“遵守”某個協(xié)議
四大類
1.CABasicAnimation:基礎(chǔ)動畫
2.CAAnimationGroup:組動畫
3.CAKeyframeAnimation:關(guān)鍵幀動畫
4.CATransition:轉(zhuǎn)場動畫
注:Core Animation是直接作用在CALayer上的,并非UIView
CAAnimation屬性介紹
- duration:動畫的持續(xù)時間
- repeatCount:動畫的重復(fù)次數(shù)
- repeatDuration:動畫的重復(fù)時間
- removedOnCompletion:默認為YES煤禽,代表動畫執(zhí)行完畢后就從圖層上移除沐鼠,圖形會恢復(fù)到動畫執(zhí)行前的狀態(tài)徽惋。如果想讓圖層保持顯示動畫執(zhí)行后的狀態(tài)回怜,那就設(shè)置為NO距辆,不過還要設(shè)置fillMode為kCAFillModeForwards
- fillMode:決定當(dāng)前對象在非active時間段的行為.比如動畫開始之前,動畫結(jié)束之后
- beginTime:可以用來設(shè)置動畫延遲執(zhí)行時間翘紊,若想延遲2s蔽氨,就設(shè)置為CACurrentMediaTime()+2,CACurrentMediaTime()為圖層的當(dāng)前時間
- timingFunction:速度控制函數(shù)帆疟,控制動畫運行的節(jié)奏
kCAMediaTimingFunctionLinear(線性):勻速鹉究,給你一個相對靜態(tài)的感覺
kCAMediaTimingFunctionEaseIn(漸進):動畫緩慢進入,然后加速離開
kCAMediaTimingFunctionEaseOut(漸出):動畫全速進入鸯匹,然后減速的到達目的地
kCAMediaTimingFunctionEaseInEaseOut(漸進漸出):動畫緩慢的進入坊饶,中間加速,然后減速的到達目的地殴蓬。這個是默認的動畫行為匿级。
CABasicAnimation
-
效果圖
代碼
import UIKit
class ViewController: UIViewController {
var layer :CALayer?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(redView)
layer = redView.layer
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let basicAni = CABasicAnimation()
basicAni.keyPath = "position.x"
// basicAni.fromValue = (10) // 從哪
// basicAni.toValue = (300) // 到哪
// basicAni.duration = 4 //動畫執(zhí)行的時間,默認0.25
basicAni.byValue = (10); // 在自身的基礎(chǔ)上累加
// 不希望核心動畫回到原來的位置
basicAni.fillMode = kCAFillModeForwards;
basicAni.removedOnCompletion = false;
self.layer?.addAnimation(basicAni, forKey: nil)
print(self.redView.frame.origin.x)
}
//懶加載redView
lazy var redView : UIView = {
let redView = UIView(frame:CGRectMake(100, 100, 100, 100))
redView.backgroundColor = UIColor.redColor()
return redView
}()
}
由打印結(jié)果可看出雖然redView的layer的frame在變化染厅,但redView的沒變
CAKeyframeAnimation
- 效果
- 代碼
import UIKit
class ViewController: UIViewController {
var layer :CALayer?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(redView)
layer = redView.layer
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let keyframeAni = CAKeyframeAnimation()
keyframeAni.keyPath = "position";
//創(chuàng)建路徑
let path = UIBezierPath(ovalInRect: CGRectMake(100, 100, 160, 160))
keyframeAni.path = path.CGPath
keyframeAni.duration = 2
keyframeAni.repeatCount = 300
self.layer?.addAnimation(keyframeAni, forKey: nil)
}
//懶加載redView
lazy var redView : UIView = {
let redView = UIView(frame:CGRectMake(100, 100, 30, 30))
redView.backgroundColor = UIColor.redColor()
return redView
}()
}
CAAnimationGroup
-
演示
代碼
import UIKit
class ViewController: UIViewController {
var layer :CALayer?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(redView)
layer = redView.layer
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let group = CAAnimationGroup()
let keyframeAni = CAKeyframeAnimation()
keyframeAni.keyPath = "position"
let path = UIBezierPath(arcCenter:CGPointMake(200, 200) , radius: 80, startAngle: 0, endAngle: CGFloat(2.0 * M_PI), clockwise: true)
keyframeAni.path = path.CGPath
let basicAni = CABasicAnimation()
basicAni.keyPath = "transform.rotation"
basicAni.byValue = (M_PI * 2 * 3)
group.animations = [keyframeAni,basicAni]
group.duration = 3
group.repeatCount = Float(INT_MAX)
self.layer?.addAnimation(group, forKey: nil)
}
//懶加載redView
lazy var redView : UIView = {
let redView = UIView(frame:CGRectMake(100, 100, 30, 30))
redView.backgroundColor = UIColor.redColor()
return redView
}()
}
CATransition
type屬性