# 圖層行為
現在來做個實驗,試著直接對UIView關聯的圖層做動畫而不是一個單獨的圖層。清單7.4是對清單7.2代碼的一點修改,移除了`colorLayer`,并且直接設置`layerView`關聯圖層的背景色。
清單7.4 直接設置圖層的屬性
~~~
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *layerView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//set the color of our layerView backing layer directly
self.layerView.layer.backgroundColor = [UIColor blueColor].CGColor;
}
- (IBAction)changeColor
{
//begin a new transaction
[CATransaction begin];
//set the animation duration to 1 second
[CATransaction setAnimationDuration:1.0];
//randomize the layer background color
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
self.layerView.layer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
//commit the transaction
[CATransaction commit];
}
~~~
運行程序,你會發現當按下按鈕,圖層顏色瞬間切換到新的值,而不是之前平滑過渡的動畫。發生了什么呢?隱式動畫好像被`UIView`關聯圖層給禁用了。
試想一下,如果`UIView`的屬性都有動畫特性的話,那么無論在什么時候修改它,我們都應該能注意到的。所以,如果說UIKit建立在Core Animation(默認對所有東西都做動畫)之上,那么隱式動畫是如何被UIKit禁用掉呢?
我們知道Core Animation通常對`CALayer`的所有屬性(可動畫的屬性)做動畫,但是`UIView`把它關聯的圖層的這個特性關閉了。為了更好說明這一點,我們需要知道隱式動畫是如何實現的。
我們把改變屬性時`CALayer`自動應用的動畫稱作*行為*,當`CALayer`的屬性被修改時候,它會調用`-actionForKey:`方法,傳遞屬性的名稱。剩下的操作都在`CALayer`的頭文件中有詳細的說明,實質上是如下幾步:
* 圖層首先檢測它是否有委托,并且是否實現`CALayerDelegate`協議指定的`-actionForLayer:forKey`方法。如果有,直接調用并返回結果。
* 如果沒有委托,或者委托沒有實現`-actionForLayer:forKey`方法,圖層接著檢查包含屬性名稱對應行為映射的`actions`字典。
* 如果`actions字典`沒有包含對應的屬性,那么圖層接著在它的`style`字典接著搜索屬性名。
* 最后,如果在`style`里面也找不到對應的行為,那么圖層將會直接調用定義了每個屬性的標準行為的`-defaultActionForKey:`方法。
所以一輪完整的搜索結束之后,`-actionForKey:`要么返回空(這種情況下將不會有動畫發生),要么是`CAAction`協議對應的對象,最后`CALayer`拿這個結果去對先前和當前的值做動畫。
于是這就解釋了UIKit是如何禁用隱式動畫的:每個`UIView`對它關聯的圖層都扮演了一個委托,并且提供了`-actionForLayer:forKey`的實現方法。當不在一個動畫塊的實現中,`UIView`對所有圖層行為返回`nil`,但是在動畫block范圍之內,它就返回了一個非空值。我們可以用一個demo做個簡單的實驗(清單7.5)
清單7.5 測試UIView的`actionForLayer:forKey:`實現
~~~
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *layerView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//test layer action when outside of animation block
NSLog(@"Outside: %@", [self.layerView actionForLayer:self.layerView.layer forKey:@"backgroundColor"]);
//begin animation block
[UIView beginAnimations:nil context:nil];
//test layer action when inside of animation block
NSLog(@"Inside: %@", [self.layerView actionForLayer:self.layerView.layer forKey:@"backgroundColor"]);
//end animation block
[UIView commitAnimations];
}
@end
~~~
運行程序,控制臺顯示結果如下:
~~~
$ LayerTest[21215:c07] Outside: <null>
$ LayerTest[21215:c07] Inside: <CABasicAnimation: 0x757f090>
~~~
于是我們可以預言,當屬性在動畫塊之外發生改變,`UIView`直接通過返回`nil`來禁用隱式動畫。但如果在動畫塊范圍之內,根據動畫具體類型返回相應的屬性,在這個例子就是`CABasicAnimation`(第八章“顯式動畫”將會提到)。
當然返回`nil`并不是禁用隱式動畫唯一的辦法,`CATransacition`有個方法叫做`+setDisableActions:`,可以用來對所有屬性打開或者關閉隱式動畫。如果在清單7.2的`[CATransaction begin]`之后添加下面的代碼,同樣也會阻止動畫的發生:
~~~
[CATransaction setDisableActions:YES];
~~~
總結一下,我們知道了如下幾點
* `UIView`關聯的圖層禁用了隱式動畫,對這種圖層做動畫的唯一辦法就是使用`UIView`的動畫函數(而不是依賴`CATransaction`),或者繼承`UIView`,并覆蓋`-actionForLayer:forKey:`方法,或者直接創建一個顯式動畫(具體細節見第八章)。
* 對于單獨存在的圖層,我們可以通過實現圖層的`-actionForLayer:forKey:`委托方法,或者提供一個`actions`字典來控制隱式動畫。
我們來對顏色漸變的例子使用一個不同的行為,通過給`colorLayer`設置一個自定義的`actions`字典。我們也可以使用委托來實現,但是`actions`字典可以寫更少的代碼。那么到底改如何創建一個合適的行為對象呢?
行為通常是一個被Core Animation*隱式*調用的*顯式*動畫對象。這里我們使用的是一個實現了`CATransaction`的實例,叫做*推進過渡*。
第八章中將會詳細解釋過渡,不過對于現在,知道`CATransition`響應`CAAction`協議,并且可以當做一個圖層行為就足夠了。結果很贊,不論在什么時候改變背景顏色,新的色塊都是從左側滑入,而不是默認的漸變效果。
清單7.6 實現自定義行為
~~~
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *layerView;
@property (nonatomic, weak) IBOutlet CALayer *colorLayer;/*熱心人發現這里應該改為@property (nonatomic, strong) CALayer *colorLayer;否則運行結果不正確。
*/
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//create sublayer
self.colorLayer = [CALayer layer];
self.colorLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
self.colorLayer.backgroundColor = [UIColor blueColor].CGColor;
//add a custom action
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
self.colorLayer.actions = @{@"backgroundColor": transition};
//add it to our view
[self.layerView.layer addSublayer:self.colorLayer];
}
- (IBAction)changeColor
{
//randomize the layer background color
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
self.colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
}
@end
~~~

圖7.3 使用推進過渡的色值動畫
- Introduction
- 1. 圖層樹
- 1.1 圖層與視圖
- 1.2 圖層的能力
- 1.3 使用圖層
- 1.4 總結
- 2. 寄宿圖
- 2.1 contents屬性
- 2.2 Custom Drawing
- 2.3 總結
- 3. 圖層幾何學
- 3.1 布局
- 3.2 錨點
- 3.3 坐標系
- 3.4 Hit Testing
- 3.5 自動布局
- 3.6 總結
- 4. 視覺效果
- 4.1 圓角
- 4.2 圖層邊框
- 4.3 陰影
- 4.4 圖層蒙板
- 4.5 拉伸過濾
- 4.6 組透明
- 4.7 總結
- 5. 變換
- 5.1 仿射變換
- 5.2 3D變換
- 5.3 固體對象
- 5.4 總結
- 6. 專用圖層
- 6.1 CAShapeLayer
- 6.2 CATextLayer
- 6.3 CATransformLayer
- 6.4 CAGradientLayer
- 6.5 CAReplicatorLayer
- 6.6 CAScrollLayer
- 6.7 CATiledLayer
- 6.8 CAEmitterLayer
- 6.9 CAEAGLLayer
- 6.10 AVPlayerLayer
- 6.11 總結
- 7. 隱式動畫
- 7.1 事務
- 7.2 完成塊
- 7.3 圖層行為
- 7.4 呈現與模型
- 7.5 總結
- 8. 顯式動畫
- 8.1 屬性動畫
- 8.2 動畫組
- 8.3 過渡
- 8.4 在動畫過程中取消動畫
- 8.5 總結
- 9. 圖層時間
- 9.1 CAMediaTiming協議
- 9.2 層級關系時間
- 9.3 手動動畫
- 9.4 總結
- 10. 緩沖
- 10.1 動畫速度
- 10.2 自定義緩沖函數
- 10.3 總結
- 11. 基于定時器的動畫
- 11.1 定時幀
- 11.2 物理模擬
- 12. 性能調優
- 12.1. CPU VS GPU
- 12.2 測量,而不是猜測
- 12.3 Instruments
- 12.4 總結
- 13. 高效繪圖
- 13.1 軟件繪圖
- 13.2 矢量圖形
- 13.3 臟矩形
- 13.4 異步繪制
- 13.5 總結
- 14. 圖像IO
- 14.1 加載和潛伏
- 14.2 緩存
- 14.3 文件格式
- 14.4 總結
- 15. 圖層性能
- 15.1 隱式繪制
- 15.2 離屏渲染
- 15.3 混合和過度繪制
- 15.4 減少圖層數量
- 15.5 總結