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