最近看了點動畫方面的知識,做個筆記記錄一下。
#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屬性,運行卻有動畫效果。
實際上動畫執行的時間取決于當前事務的設置,動畫類型取決于圖層行為。
事務是通過CATransaction類來做管理,CATransaction沒有屬性或者實例方法,并且也不能用+alloc和-init方法創建它。但是可以用+begin和+commit分別來入棧或者出棧。任何可以做動畫的圖層屬性都會被添加到棧頂的事務,你可以通過+setAnimationDuration:方法設置當前事務的動畫時間,或者通過+animationDuration方法來獲取值(默認0.25秒)。
Core Animation在每個run loop周期中自動開始一次新的事務(run loop是iOS負責收集用戶輸入,處理定時器或者網絡事件并且重新繪制屏幕的東西),即使你不顯式的用[CATransaction begin]開始一次事務,任何在一次run loop循環中屬性的改變都會被集中起來,然后做一次0.25秒的動畫。
所以事務是通過CATransaction類隱式得設置了動畫執行時間,我們也可以通過setAnimationDuration設置動畫時間。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 如果我們要自己通過setAnimationDuration設置動畫執行時間,必須要先起一個新的事務,因為修改當前事務的時間可能會導致同一時刻別的動畫,所以最好是在調整動畫之前壓入一個新的事務。
[CATransaction begin];
// 默認為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方法類似。實際上在+beginAnimations:context:和+commitAnimations之間所有視圖或者圖層屬性的改變而做的動畫都是由于設置了CATransaction的原因。
UIView的+animateWithDuration:animations:提供了一個block允許在動畫結束后做一些操作,CATransaction也提供了一個+setCompletionBlock:方法。
關于隱式動畫,還有最重要的一點是:rootLayer不執行隱式動畫。
- (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;
}
點擊屏幕,圖層顏色是瞬間切換的,沒有了動畫效果。我們知道動畫類型取決于圖層行為,圖層行為是
我們把改變屬性時CALayer自動應用的動畫稱作行為,當CALayer的屬性被修改時候,它會調用-actionForKey:方法,傳遞屬性的名稱。剩下的操作都在CALayer的頭文件中有詳細的說明,實質上是如下幾步:
- 圖層首先檢測它是否有委托,并且是否實現CALayerDelegate協議指定的-actionForLayer:forKey方法。如果有,直接調用并返回結果。
- 如果沒有委托,或者委托沒有實現-actionForLayer:forKey方法,圖層接著檢查包含屬性名稱對應行為映射的actions字典。
- 如果actions字典沒有包含對應的屬性,那么圖層接著在它的style字典接著搜索屬性名。
- 最后,如果在style里面也找不到對應的行為,那么圖層將會直接調用定義了每個屬性的標準行為的-defaultActionForKey:方法。
所以一輪完整的搜索結束之后,-actionForKey:要么返回空(這種情況下將不會有動畫發生),要么是CAAction協議對應的對象,最后CALayer拿這個結果去對先前和當前的值做動畫。
于是這就解釋了UIKit是如何禁用隱式動畫的:每個UIView對它關聯的圖層都扮演了一個委托,并且提供了-actionForLayer:forKey的實現方法。當不在一個動畫塊的實現中,UIView對所有圖層行為返回nil,但是在動畫block范圍之內,它就返回了一個非空值。
我們可以測試一下:
- (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,這種情況下就不執行動畫。我們也可以通過[CATransaction setDisableActions:YES];阻止動畫執行。
行為通常是一個被Core Animation隱式調用的顯式動畫對象。
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
self.colorLayer.actions = @{@"backgroundColor": transition};
[self.contentView.layer addSublayer:self.colorlayer];
需要注意的是動畫效果的代碼要在layer添加之前。