最近看了點(diǎn)動畫方面的知識,做個筆記記錄一下岭洲。
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (nonatomic,strong) CALayer *colorlayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.colorlayer = [CALayer layer];
self.colorlayer.frame = CGRectMake(0, 0, 100, 100);
self.colorlayer.backgroundColor = [UIColor redColor].CGColor;
[self.contentView.layer addSublayer:self.colorlayer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGFloat red = arc4random_uniform(256) / 255.0;
CGFloat gre = arc4random_uniform(256) / 255.0;
CGFloat blu = arc4random_uniform(256) / 255.0;
self.colorlayer.backgroundColor = [UIColor colorWithRed:red green:gre blue:blu alpha:1.0].CGColor;
}
這就是隱式動畫馋缅,僅僅改變了Layer的backgroundColor屬性取刃,運(yùn)行卻有動畫效果。
實(shí)際上動畫執(zhí)行的時間取決于當(dāng)前事務(wù)的設(shè)置待侵,動畫類型取決于圖層行為丢早。
事務(wù)是通過CATransaction類來做管理,CATransaction沒有屬性或者實(shí)例方法秧倾,并且也不能用+alloc和-init方法創(chuàng)建它怨酝。但是可以用+begin和+commit分別來入棧或者出棧那先。任何可以做動畫的圖層屬性都會被添加到棧頂?shù)氖聞?wù)农猬,你可以通過+setAnimationDuration:方法設(shè)置當(dāng)前事務(wù)的動畫時間,或者通過+animationDuration方法來獲取值(默認(rèn)0.25秒)售淡。
Core Animation在每個run loop周期中自動開始一次新的事務(wù)(run loop是iOS負(fù)責(zé)收集用戶輸入斤葱,處理定時器或者網(wǎng)絡(luò)事件并且重新繪制屏幕的東西),即使你不顯式的用[CATransaction begin]開始一次事務(wù)揖闸,任何在一次run loop循環(huán)中屬性的改變都會被集中起來揍堕,然后做一次0.25秒的動畫。
所以事務(wù)是通過CATransaction類隱式得設(shè)置了動畫執(zhí)行時間楔壤,我們也可以通過setAnimationDuration設(shè)置動畫時間鹤啡。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 如果我們要自己通過setAnimationDuration設(shè)置動畫執(zhí)行時間,必須要先起一個新的事務(wù)蹲嚣,因?yàn)樾薷漠?dāng)前事務(wù)的時間可能會導(dǎo)致同一時刻別的動畫递瑰,所以最好是在調(diào)整動畫之前壓入一個新的事務(wù)祟牲。
[CATransaction begin];
// 默認(rèn)為0.25秒
[CATransaction setAnimationDuration:0.25];
CGFloat red = arc4random_uniform(256) / 255.0;
CGFloat gre = arc4random_uniform(256) / 255.0;
CGFloat blu = arc4random_uniform(256) / 255.0;
self.colorlayer.backgroundColor = [UIColor colorWithRed:red green:gre blue:blu alpha:1.0].CGColor;
[CATransaction commit];
}
UIView有兩個方法,+beginAnimations:context:和+commitAnimations抖部、+animateWithDuration:animations:说贝,和CATransaction的+begin和+commit方法類似。實(shí)際上在+beginAnimations:context:和+commitAnimations之間所有視圖或者圖層屬性的改變而做的動畫都是由于設(shè)置了CATransaction的原因慎颗。
UIView的+animateWithDuration:animations:提供了一個block允許在動畫結(jié)束后做一些操作乡恕,CATransaction也提供了一個+setCompletionBlock:方法。
關(guān)于隱式動畫俯萎,還有最重要的一點(diǎn)是:rootLayer不執(zhí)行隱式動畫傲宜。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.contentView.layer.backgroundColor = [UIColor redColor].CGColor;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGFloat red = arc4random_uniform(256) / 255.0;
CGFloat gre = arc4random_uniform(256) / 255.0;
CGFloat blu = arc4random_uniform(256) / 255.0;
self.contentView.layer.backgroundColor = [UIColor colorWithRed:red green:gre blue:blu alpha:1.0].CGColor;
}
點(diǎn)擊屏幕,圖層顏色是瞬間切換的夫啊,沒有了動畫效果函卒。我們知道動畫類型取決于圖層行為,圖層行為是
我們把改變屬性時CALayer自動應(yīng)用的動畫稱作行為撇眯,當(dāng)CALayer的屬性被修改時候报嵌,它會調(diào)用-actionForKey:方法,傳遞屬性的名稱熊榛。剩下的操作都在CALayer的頭文件中有詳細(xì)的說明锚国,實(shí)質(zhì)上是如下幾步:
- 圖層首先檢測它是否有委托,并且是否實(shí)現(xiàn)CALayerDelegate協(xié)議指定的-actionForLayer:forKey方法玄坦。如果有血筑,直接調(diào)用并返回結(jié)果。
- 如果沒有委托营搅,或者委托沒有實(shí)現(xiàn)-actionForLayer:forKey方法云挟,圖層接著檢查包含屬性名稱對應(yīng)行為映射的actions字典。
- 如果actions字典沒有包含對應(yīng)的屬性转质,那么圖層接著在它的style字典接著搜索屬性名园欣。
- 最后,如果在style里面也找不到對應(yīng)的行為休蟹,那么圖層將會直接調(diào)用定義了每個屬性的標(biāo)準(zhǔn)行為的-defaultActionForKey:方法沸枯。
所以一輪完整的搜索結(jié)束之后,-actionForKey:要么返回空(這種情況下將不會有動畫發(fā)生)赂弓,要么是CAAction協(xié)議對應(yīng)的對象绑榴,最后CALayer拿這個結(jié)果去對先前和當(dāng)前的值做動畫。
于是這就解釋了UIKit是如何禁用隱式動畫的:每個UIView對它關(guān)聯(lián)的圖層都扮演了一個委托盈魁,并且提供了-actionForLayer:forKey的實(shí)現(xiàn)方法翔怎。當(dāng)不在一個動畫塊的實(shí)現(xiàn)中,UIView對所有圖層行為返回nil,但是在動畫block范圍之內(nèi)赤套,它就返回了一個非空值飘痛。
我們可以測試一下:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.contentView.layer.backgroundColor = [UIColor redColor].CGColor;
NSLog(@"1 --- %@",[self.contentView actionForLayer:self.contentView.layer forKey:@"backgroundColor"]);
[UIView beginAnimations:nil context:nil];
NSLog(@"2 --- %@",[self.contentView actionForLayer:self.contentView.layer forKey:@"backgroundColor"]);
[UIView commitAnimations];
}
2017-04-11 11:05:22.758 隱式動畫[3583:3064040] 1 --- <null>
2017-04-11 11:05:22.759 隱式動畫[3583:3064040] 2 --- <CABasicAnimation: 0x61000003da00>```
所以-actionForKey:返回nil,這種情況下就不執(zhí)行動畫容握。我們也可以通過[CATransaction setDisableActions:YES];阻止動畫執(zhí)行宣脉。
行為通常是一個被Core Animation隱式調(diào)用的顯式動畫對象。
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
self.colorLayer.actions = @{@"backgroundColor": transition};
[self.contentView.layer addSublayer:self.colorlayer];
需要注意的是動畫效果的代碼要在layer添加之前剔氏。