layer actions的觸發一般發生在:layer從layer-tree上插入或者刪除,layer屬性值被改變,或者應用程序顯式的請求。通常的anction被觸發導致動畫顯示。
1.action對象的作用
action對象通過CAAction協議去響應一個action identifier。action identifier 用標準的點分隔關鍵路徑命名。一個layer負責去映射action identifier 給一個action object。當有identifier的action object 被找到時,發送在CAAction協議裏定義的消息。
CALayer類提供了一個默認的action object----CAAnimation實例,其是個兼容CAAction協議的類---對所有的可動畫的layer屬性。CALayer還定義了一些action triggers,這些沒有直接聯繫到屬性上。
action trigger 和對應的identifiers
Trigger |
Action identifier |
---|---|
A layer is inserted into a visible layer-tree, or the |
The action identifier constant |
A layer is removed from a visible layer-tree, or the |
The action identifier constant |
A layer replaces an existing layer in a visible layer tree using |
The action identifier constant |
2.定義action keys的搜索模式
當一個action trigger發生時,layer的方法actionForKey:會被調用。此方法會返回一個與傳入的action identifier參數對應的action object,如果沒有存在的action object,則返回nil。
當CALayer對於一個identifier實現actionForKey:,將使用下面的搜索模式:
(1)如果layer有代理,並且實現了方法actionForLayer:forKey:,此方法會被調用,傳遞的參數爲layer和action identifier。代理應該遵循下面的情況去實現此方法。
返回一個和action identifier對應的action object。
如果不處理action identifier,則返回nil。
如果不處理acttion identifier並且搜索應該被終止,則返回NSNull。
(2)查找layer的actions屬性,此屬性爲一個字典,尋找對應於action identifier的對象。
(3)查找layer的style屬性,此屬性也爲一個字典,尋找對應於action identifier的對象。
(4)layer類發送消息defaultActionForKey:,它會返回一個對應與action identifier的action object,沒有發現則返回nil。