CABasicAnimation 基本動(dòng)畫
動(dòng)畫的本質(zhì)就是修改圖層的某些屬性
核心動(dòng)畫,利用核心動(dòng)畫修改Layer某些屬性
-
簡(jiǎn)介
- 基本動(dòng)畫是
CAPropertyAnimation
的子類
- 基本動(dòng)畫是
-
屬性說明:
-
fromValue
:keyPath
相應(yīng)屬性的初始值 -
toValue
:keyPath
相對(duì)應(yīng)的結(jié)束值
-
-
動(dòng)畫過程說明:
- 隨著動(dòng)畫的進(jìn)行,在長度為
duration
的持續(xù)時(shí)間內(nèi)顿苇,keyPath
相應(yīng)的屬性值從fromValue
漸漸變?yōu)?code>toValue -
keyPath
內(nèi)容是CALayer
的可動(dòng)畫Animatable
屬性 - 如果
fillMode=kCAFillModeForwards
同時(shí)removedOnComletion=NO
葵第,那么在動(dòng)畫執(zhí)行完畢后,圖層會(huì)保持顯示動(dòng)畫執(zhí)行后的狀態(tài)嘴秸。毁欣。但在實(shí)質(zhì)上,圖層的屬性值還是動(dòng)畫執(zhí)行前的初始值岳掐,并沒有真正被改變凭疮。
- 隨著動(dòng)畫的進(jìn)行,在長度為
-
以下是代碼實(shí)現(xiàn)步驟:
- 1.創(chuàng)建基礎(chǔ)核心動(dòng)畫
CABasicAnimation *anim = [CABasicAnimation animation];
- 2.描述修改Layer的某個(gè)屬性(@"transform.rotation"...)
anim.keyPath = @"position”;
- 3.描述修改Layer屬性的值
- 動(dòng)畫的起點(diǎn)(如果不設(shè),以默認(rèn)值為終點(diǎn))
anim.fromValue = @0
- 動(dòng)畫的終點(diǎn)
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 400)];
- 4.動(dòng)畫時(shí)長
anim.duration = 1;
- 5.取消反彈
- (1)在動(dòng)畫完成的時(shí)候不要給我把動(dòng)畫銷毀
anim.removedOnCompletion = NO;
- (2)動(dòng)畫永遠(yuǎn)保持最新的狀態(tài)
anim.fillMode = kCAFillModeForwards;
- (1)在動(dòng)畫完成的時(shí)候不要給我把動(dòng)畫銷毀
- 6.添加核心動(dòng)畫
[_reaView.layer addAnimation:anim forKey:nil];
- 1.創(chuàng)建基礎(chǔ)核心動(dòng)畫
CAKeyframeAnimation——關(guān)鍵幀動(dòng)畫
-
簡(jiǎn)介
- 關(guān)鍵幀動(dòng)畫串述,也是
CAPropertyAnimation
的子類执解, - 與
CABasicAnimation
的區(qū)別是:-
CABasicAnimation
只能從一個(gè)數(shù)值(fromValue
)變到另一個(gè)數(shù)值(toValue
),而CAKeyframeAnimation
會(huì)使用一個(gè)NSArray
保存這些數(shù)值
-
- 關(guān)鍵幀動(dòng)畫串述,也是
-
屬性說明:
-
values
:上述的NSArray對(duì)象纲酗。里面的元素稱為“關(guān)鍵幀”(keyframe)衰腌。動(dòng)畫對(duì)象會(huì)在指定的時(shí)間(duration)內(nèi),依次顯示values數(shù)組中的每一個(gè)關(guān)鍵幀 -
path
:可以設(shè)置一個(gè)CGPathRef觅赊、CGMutablePathRef右蕊,讓圖層按照路徑軌跡移動(dòng)。path只對(duì)CALayer的anchorPoint和position起作用茉兰。如果設(shè)置了path尤泽,那么values將被忽略
-
keyTimes
:可以為對(duì)應(yīng)的關(guān)鍵幀指定對(duì)應(yīng)的時(shí)間點(diǎn),其取值范圍為0到1.0,keyTimes中的每一個(gè)時(shí)間值都對(duì)應(yīng)values中的每一幀坯约。如果沒有設(shè)置keyTimes熊咽,各個(gè)關(guān)鍵幀的時(shí)間是平分的
-
CABasicAnimation
可看做是只有2個(gè)關(guān)鍵幀的CAKeyframeAnimation
-
代碼實(shí)現(xiàn)思路與步驟:
- 1.初始化幀動(dòng)畫
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
- 2.描述修改layer的屬性,
- transform.rotation只能二維旋轉(zhuǎn)
anim.keyPath = @"transform.rotation";
- position
anim.keyPath = @"position";
- transform.rotation只能二維旋轉(zhuǎn)
- 3.修改Layer值
anim.values = @[@(angle2Radion(-5)),@(angle2Radion(5)),@(angle2Radion(-5))];
- 創(chuàng)建路徑
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:_imageView.center radius:100 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
- 4.設(shè)置動(dòng)畫的屬性值
- 設(shè)置動(dòng)畫執(zhí)行時(shí)間
anim.duration = 2;
- 動(dòng)畫執(zhí)行次數(shù)
anim.repeatCount = MAXFLOAT;
- 設(shè)置動(dòng)畫執(zhí)行時(shí)間
- 5.添加動(dòng)畫到圖層(Layer)
[_imageView.layer addAnimation:anim forKey:nil];
- 1.初始化幀動(dòng)畫
CAAnimationGroup——?jiǎng)赢嫿M
-
簡(jiǎn)介
- 動(dòng)畫組,是
CAAnimation
的子類闹丐,可以保存一組動(dòng)畫對(duì)象横殴,將CAAnimationGroup
對(duì)象加入層后,組中所有動(dòng)畫對(duì)象可以同時(shí)并發(fā)運(yùn)行
- 動(dòng)畫組,是
-
屬性說明:
-
animations
:用來保存一組動(dòng)畫對(duì)象的NSArray
默認(rèn)情況下卿拴,一組動(dòng)畫對(duì)象是同時(shí)運(yùn)行的衫仑,也可以通過設(shè)置動(dòng)畫對(duì)象的beginTime
屬性來更改動(dòng)畫的開始時(shí)間
-
CATransition——轉(zhuǎn)場(chǎng)動(dòng)畫
- 簡(jiǎn)介:
-
CATransition
是CAAnimation
的子類,用于做轉(zhuǎn)場(chǎng)動(dòng)畫堕花,能夠?yàn)閷犹峁┮瞥銎聊缓鸵迫肫聊坏膭?dòng)畫效果文狱。iOS比Mac OS X的轉(zhuǎn)場(chǎng)動(dòng)畫效果少一點(diǎn)
-
-
UINavigationController
就是通過`CATransition實(shí)現(xiàn)將控制器的視圖推入(puch)屏幕的動(dòng)畫效果 - 動(dòng)畫屬性:
-
type
:動(dòng)畫過渡類型 -
subtype
:動(dòng)畫過渡方向 -
startProgress
:動(dòng)畫起點(diǎn)(在整體動(dòng)畫的百分比) -
endProgress
:動(dòng)畫終點(diǎn)(在整體動(dòng)畫的百分比)
-
- 代碼實(shí)現(xiàn)思路與步驟:
-
注意點(diǎn)
:- 只要切換界面 都可以使用轉(zhuǎn)場(chǎng)動(dòng)畫
- 誰切換界面 就添加到誰上
- 轉(zhuǎn)場(chǎng)動(dòng)畫代碼必須和界面切換的代碼放在一起
- 1.寫界面切換的代碼(切換界面)
//設(shè)置在touchBegin方法中 static int i = 2; NSString *imageName = [NSString stringWithFormat:@"%d",i]; _imageView.image = [UIImage imageNamed:imageName]; i++;`
- 2.初始化轉(zhuǎn)場(chǎng)動(dòng)畫
CATransition *anim = [CATransition animation];
- 3.指定轉(zhuǎn)場(chǎng)類型(其他樣式見過度效果)
anim.type = @"pageCurl";
- 4.設(shè)置轉(zhuǎn)場(chǎng)動(dòng)畫屬性
- (1)設(shè)置動(dòng)畫開始的進(jìn)度
anim.startProgress = 0.5;
- (2)設(shè)置動(dòng)畫結(jié)束的進(jìn)度
anim.endProgress = 0.8;
- (3)設(shè)置動(dòng)畫的時(shí)間
anim.duration = 3;
- (1)設(shè)置動(dòng)畫開始的進(jìn)度
- 5.添加轉(zhuǎn)場(chǎng)動(dòng)畫到圖層
[_imageView.layer addAnimation:anim forKey:nil];
-
轉(zhuǎn)場(chǎng)動(dòng)畫過渡效果
CADisplayLink定時(shí)器
CADisplayLink
是一種以屏幕刷新頻率觸發(fā)的時(shí)鐘機(jī)制,每秒鐘執(zhí)行大約60次左右缘挽,可以暫停CADisplayLink
定時(shí)器CADisplayLink
是一個(gè)計(jì)時(shí)器瞄崇,可以使繪圖代碼與視圖的刷新頻率保持同步,而NSTimer
無法確保計(jì)時(shí)器實(shí)際被觸發(fā)的準(zhǔn)確時(shí)間壕曼,無法暫停-
使用方法:
- 定義
CADisplayLink
并制定觸發(fā)調(diào)用方法 - 將顯示鏈接添加到主運(yùn)行循環(huán)隊(duì)列
- 定義
-
代碼實(shí)現(xiàn)步驟:
- 1.由于定時(shí)器只需要?jiǎng)?chuàng)建一次苏研,因此定義為成員屬性
@property (nonatomic, weak) CADisplayLink *link;
- 2.使用懶加載link屬性
- (CADisplayLink *)link { if (_link == nil) { //(1)定義CADisplayLink并制定觸發(fā)調(diào)用方法 CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(timeChange)]; _link = link; //(2)加到主運(yùn)行循環(huán)隊(duì)列 [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; } return _link; }
-
3.實(shí)現(xiàn)觸發(fā)調(diào)用方法
// 1秒調(diào)用60次 每次旋轉(zhuǎn)45 / 60.0度 - (void)timeChange { _wheelView.transform = CGAffineTransformRotate(_wheelView.transform, (45 / 60.0) / 180.0 * M_PI); }
4.開啟定時(shí)器
self.link.paused = NO;
(YES為暫停)
- 1.由于定時(shí)器只需要?jiǎng)?chuàng)建一次苏研,因此定義為成員屬性