CoreAnimation编程指南(七)图层Action
生活随笔
收集整理的這篇文章主要介紹了
CoreAnimation编程指南(七)图层Action
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
圖層的行為在以下情況發生的時候被觸發:從圖層樹里面插入或者刪除一個圖層,圖層的屬性值被修改了,或者程序顯式要求。通常情況下,行為觸發器是動畫顯示的結果所在。 ? 1.1 行為對象的角色
? 一個行為對象是一個通過CAAction協議響應行為標識符的對象。行為標識符使用標準圓點分隔的關鍵路徑來命名。圖層負責把行為標識符映射到特定的行為對象。當一個特定標識符的行為對象被確定的時候,它會發送一個CAAction協議定義的消息。
? CALayer類提供了默認的CAAnimation的行為對象實例,一個兼容類所有動畫層屬性CAAction協議。表1中CALayer同樣定義了以下沒有直接對應到屬性的行為觸發器和他們的行為標識符。
? Action觸發器和相應的標示符: ? (1)一個layer被插入一個可見的layer樹,或者layer的hidden屬性被設為NO ? ? ?kCAOnOrderIn ? (2)一個layer被從一個可見的layer樹中移除,或者layer的hidden屬性被設為YES ? ? ?kCAOnOrderOut ? (3)使用replaceSublayer:with:方法將一個可見樹中的layer替換 ? ? ?kCATransition ? 1.2 已定義搜索模式的行為鍵值
? 當一個行為觸發器發生的時候,圖層的actionForKey:方法被調用。此方法返回一個行為對象,對應的標識符作為參數,或如果行為對象不存在的話返回nil。 ?? ? 當CALayer為一個標識符實現的actionForKey:方法被調用的時候,以下的搜索模式將會被用到: ? 1.如果一個圖層有委托,那方法actionForLayer:forKey:的實現將會被調用,把圖層和行為標識符作為參數。委托的actionForLayer:forKey:的實現需要響應如下: ? (1)返回一個行為標識符對應的行為對象。 ? (2)返回nil,當無法處理行為標識符的時候。 ? (3)返回NSNull,當無法處理行為標識符,而且搜索需要被終止的時候。 ? 2.圖層的actions字典被搜索以便找到一個和行為標識符對應的對象。 ? 3.圖層的style屬性被搜索以便找到一個包含行為標識符的actions字典。 ? 4.圖層類發生一個defaultActionForKey:的消息。它將會返回一個和標識符對應的行為對象,如果不存在的話則返回nil。 ? 1.3 采用CAAction協議
? CAAction協議定義了行為對象如何被調用。實現CAAction協議的類包含一個方法runActionForKey:object:arguments:。
? 當行為對象收到一個runActionForKey:object:arguments:的消息時,行為標識符、行為發生所在的圖層、額外的參數字典會被作為參數傳遞給方法。
? 通常行為對象是CAAnimation的子類實例,它實現了CAAction協議。然而你也可以返回任何實現了CAAction協議的類對象。當實例收到runActionForKey:object:arguments:的消息時,它需要執行相應的行為。
? 當CAAnimation實例受到消息runActionForKey:object:arguments:的時候,它把自己添加到圖層的動力里面,觸發動畫的執行(查看代碼1)。 ?? ? 代碼 1 ?runActionForKey:object:arguments: 的實現:啟動動畫 ? - (void)runActionForKey:(NSString *)key object:(id)anObject arguments:(NSDictionary *)dict{ ? ? ?[(CALayer *)anObject addAnimation:self forKey:key]; ? } ? 1.4 重載隱式動畫
? 你可以為行為標識符提供隱式的動畫,通過插入一個CAAnimation的實例到style字典里面的actions的字典里面,通過實現委托方法actionForLayer:forKey:或者繼承圖層類并重載defaultActionForKey:方法返回一個相應的行為對象。
? 代碼2的示例通過委托替換contents屬性的隱式動畫。 ? 代碼 2 ? contents 屬性的隱式動畫 ? - (id<CAAction>)actionForLayer:(CALayer *)theLayer forKey:(NSString *)theKey{ ? ? CATransition *theAnimation=nil; ? ? if ([theKey isEqualToString:@"contents"]){ ? ? ? ? theAnimation = [[CATransition alloc] init]; ? ? ? ? theAnimation.duration = 1.0; ? ? ? ? theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; ? ? ? ? theAnimation.type = kCATransitionPush; ? ? ? ? theAnimation.subtype = kCATransitionFromRight; ? ? } ? ? return theAnimation; ? } ? ? 代碼3的示例使用actions字典模式禁用sublayers屬性的默認動畫。 ? 代碼 3 ?sublayers 屬性的隱式動畫 ? // get a mutable version of the current actions dictionary ? NSMutableDictionary *customActions=[NSMutableDictionary dictionaryWithDictionary:[theLayer actions]];
? // add the new action for sublayers ? [customActions setObject:[NSNull null] forKey:@"sublayers"];
? // set theLayer actions to the updated dictionary ? theLayer.actions=customActions; ? 1.5 暫時禁用行為 ?? ? 默認情況下,你任何時候改變一個可動畫顯示的屬性時,相應的動畫將會伴隨發生。
? 在修改圖層屬性的時候,你可以通過使用事務暫時禁用行為。查看“暫時禁用圖層的行為”部分來獲取更多信息。
轉自夢維:http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-layer-g-action.html
? 一個行為對象是一個通過CAAction協議響應行為標識符的對象。行為標識符使用標準圓點分隔的關鍵路徑來命名。圖層負責把行為標識符映射到特定的行為對象。當一個特定標識符的行為對象被確定的時候,它會發送一個CAAction協議定義的消息。
? CALayer類提供了默認的CAAnimation的行為對象實例,一個兼容類所有動畫層屬性CAAction協議。表1中CALayer同樣定義了以下沒有直接對應到屬性的行為觸發器和他們的行為標識符。
? Action觸發器和相應的標示符: ? (1)一個layer被插入一個可見的layer樹,或者layer的hidden屬性被設為NO ? ? ?kCAOnOrderIn ? (2)一個layer被從一個可見的layer樹中移除,或者layer的hidden屬性被設為YES ? ? ?kCAOnOrderOut ? (3)使用replaceSublayer:with:方法將一個可見樹中的layer替換 ? ? ?kCATransition ? 1.2 已定義搜索模式的行為鍵值
? 當一個行為觸發器發生的時候,圖層的actionForKey:方法被調用。此方法返回一個行為對象,對應的標識符作為參數,或如果行為對象不存在的話返回nil。 ?? ? 當CALayer為一個標識符實現的actionForKey:方法被調用的時候,以下的搜索模式將會被用到: ? 1.如果一個圖層有委托,那方法actionForLayer:forKey:的實現將會被調用,把圖層和行為標識符作為參數。委托的actionForLayer:forKey:的實現需要響應如下: ? (1)返回一個行為標識符對應的行為對象。 ? (2)返回nil,當無法處理行為標識符的時候。 ? (3)返回NSNull,當無法處理行為標識符,而且搜索需要被終止的時候。 ? 2.圖層的actions字典被搜索以便找到一個和行為標識符對應的對象。 ? 3.圖層的style屬性被搜索以便找到一個包含行為標識符的actions字典。 ? 4.圖層類發生一個defaultActionForKey:的消息。它將會返回一個和標識符對應的行為對象,如果不存在的話則返回nil。 ? 1.3 采用CAAction協議
? CAAction協議定義了行為對象如何被調用。實現CAAction協議的類包含一個方法runActionForKey:object:arguments:。
? 當行為對象收到一個runActionForKey:object:arguments:的消息時,行為標識符、行為發生所在的圖層、額外的參數字典會被作為參數傳遞給方法。
? 通常行為對象是CAAnimation的子類實例,它實現了CAAction協議。然而你也可以返回任何實現了CAAction協議的類對象。當實例收到runActionForKey:object:arguments:的消息時,它需要執行相應的行為。
? 當CAAnimation實例受到消息runActionForKey:object:arguments:的時候,它把自己添加到圖層的動力里面,觸發動畫的執行(查看代碼1)。 ?? ? 代碼 1 ?runActionForKey:object:arguments: 的實現:啟動動畫 ? - (void)runActionForKey:(NSString *)key object:(id)anObject arguments:(NSDictionary *)dict{ ? ? ?[(CALayer *)anObject addAnimation:self forKey:key]; ? } ? 1.4 重載隱式動畫
? 你可以為行為標識符提供隱式的動畫,通過插入一個CAAnimation的實例到style字典里面的actions的字典里面,通過實現委托方法actionForLayer:forKey:或者繼承圖層類并重載defaultActionForKey:方法返回一個相應的行為對象。
? 代碼2的示例通過委托替換contents屬性的隱式動畫。 ? 代碼 2 ? contents 屬性的隱式動畫 ? - (id<CAAction>)actionForLayer:(CALayer *)theLayer forKey:(NSString *)theKey{ ? ? CATransition *theAnimation=nil; ? ? if ([theKey isEqualToString:@"contents"]){ ? ? ? ? theAnimation = [[CATransition alloc] init]; ? ? ? ? theAnimation.duration = 1.0; ? ? ? ? theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; ? ? ? ? theAnimation.type = kCATransitionPush; ? ? ? ? theAnimation.subtype = kCATransitionFromRight; ? ? } ? ? return theAnimation; ? } ? ? 代碼3的示例使用actions字典模式禁用sublayers屬性的默認動畫。 ? 代碼 3 ?sublayers 屬性的隱式動畫 ? // get a mutable version of the current actions dictionary ? NSMutableDictionary *customActions=[NSMutableDictionary dictionaryWithDictionary:[theLayer actions]];
? // add the new action for sublayers ? [customActions setObject:[NSNull null] forKey:@"sublayers"];
? // set theLayer actions to the updated dictionary ? theLayer.actions=customActions; ? 1.5 暫時禁用行為 ?? ? 默認情況下,你任何時候改變一個可動畫顯示的屬性時,相應的動畫將會伴隨發生。
? 在修改圖層屬性的時候,你可以通過使用事務暫時禁用行為。查看“暫時禁用圖層的行為”部分來獲取更多信息。
轉自夢維:http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-layer-g-action.html
總結
以上是生活随笔為你收集整理的CoreAnimation编程指南(七)图层Action的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eclipse mat 分析dump文件
- 下一篇: 通过终端,查看sqlite3的存储文件