一、簡介
- CABasicAnimation是
CAPropertyAnimation
的子類, CAPropertyAnimation有一個字符串類型的keyPath
屬性
結構圖.png
keyPath內容是CALayer的可動畫Animatable屬性
,可動畫屬性可見CAlayer篇-
我們可以指定CALayer的某個屬性名為keyPath,并且對CALayer的這個屬性的值進行修改,達到相應的動畫效果。
- 例如:指定keyPath = @"position",就會修改CALayer的position屬性的值,- > 可以實現平移的動畫效果
屬性說明: fromValue:keyPath相應屬性的初始值,ntoValue:keyPath相應屬性的結束值
因此,初始化好CAPropertyAnimation的子類對象后,必須先設置keyPath(修改的是CALayer的哪個屬性)-> 指明執行的是怎樣的動畫(平移/縮放/旋轉等)
動畫過程說明:
- 隨著動畫的進行,在長度為
duration
的持續時間內,keyPath
相應屬性的值從fromValue
漸漸地變為toValue
-
keyPath
內容是CALayer的可動畫Animatable
屬性 - 如果
fillMode=kCAFillModeForwards
同時removedOnComletion=NO
,那么在動畫執行完畢后,圖層會保持顯示動畫執行后的狀態。但在實質上,圖層的屬性值還是動畫執行前的初始值,并沒有真正被改變。
二、應用
1、縮放動畫
多種方案,這里實現三種就。
-
方案一:CABasicAnimation animationWithKeyPath:@"bounds"
- layer會從原來的尺寸變為30x30
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];
anim.duration = 2;
anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 30, 30)];
[_myView.layer addAnimation:anim forKey:nil];
- 方案二:CABasicAnimation animationWithKeyPath:@"transform"
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1.5; // 動畫持續1.5s
// CALayer的寬度從0.5倍變為2倍
// CALayer的高度從0.5倍變為1.5倍
anim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1)];
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 1.5, 1)];
[_myView.layer addAnimation:anim forKey:nil];
- 方案三:anim.keyPath = @"transform.scale";
// 創建CABasicAnimation
CABasicAnimation *anim = [CABasicAnimation animation];
// 告訴系統修改圖層的哪個屬性
anim.keyPath = @"transform.scale";
// 告訴系統修改圖層的哪個值
// anim.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
anim.toValue = @0.5;
// 取消反彈
// 告訴在動畫結束的時候不要移除
anim.removedOnCompletion = NO;
// 始終保持最新的效果
anim.fillMode = kCAFillModeForwards;
[_redView.layer addAnimation:anim forKey:nil];
2、平移動畫
多種方式實現
-
2.1方式一:CABasicAnimation -> anim.keyPath = @"position";
- 注意:該方式動畫執行完畢后,并沒有真正改變CALayer的position屬性的值,
一切都是假象,并不會真實修改layer的屬性
,不會平移到(250,500)
- 注意:該方式動畫執行完畢后,并沒有真正改變CALayer的position屬性的值,
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"position";
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 500)];
// 必須設置代理
anim.delegate = self;
// 取消反彈
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[_redView.layer addAnimation:anim forKey:nil];
- 2.2 CABasicAnimation -> animationWithKeyPath:@"transform"
- 注意:通過CALayer的transform屬性實現平移動畫,layer會從自己的初始位置平移到(350, 350)位置
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1;
CATransform3D form = CATransform3DMakeTranslation(350, 350, 0);
anim.toValue = [NSValue valueWithCATransform3D:form];
[_myView.layer addAnimation:anim forKey:nil];
- 2.3 _redView.layer.position :修改layer的可動畫屬性,UIView 的方法animateWithDuration:.......執行動畫
- 該方式,實現了真正的平移到(250,500)
[UIView animateWithDuration:0.25 animations:^{
_redView.layer.position = CGPointMake(250, 500);
} completion:^(BOOL finished) {
NSLog(@"%@", NSStringFromCGPoint(_redView.layer.position));
}];
3、旋轉動畫
- 這里就例舉一種實現方案了。
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1.5;
// 繞著(0, 0, 1)這個向量軸 Z 軸,順時針旋轉45°(M_PI_4)
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];
[_redView.layer addAnimation:anim forKey:nil];