CoreAnimation编程指南(六)动画 转自:http://www.dreamingwish.com/
http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-f-animation.html
?
動畫是當(dāng)今用戶界面的關(guān)鍵因素。當(dāng)使用核心動畫的時(shí)候,動畫是自動完成的。沒有動畫的循環(huán)和計(jì)數(shù)器。你的應(yīng)用程序不負(fù)負(fù)責(zé)重繪,也不負(fù)責(zé)跟蹤動畫的當(dāng)前狀態(tài)。動畫在獨(dú)立線程里面自動執(zhí)行,沒有和你的應(yīng)用程序交互。
本章提供了對動畫類的概覽,和介紹如何創(chuàng)建隱式的和顯式的動畫。
?
1.1 動畫類和時(shí)序
核心動畫提供了一套你可以在你應(yīng)用程序里面使用的動畫類的表現(xiàn):
- CABasicAnimation提供了在圖層的屬性值間簡單的插入。
- CAKeyframeAnimation提供支持關(guān)鍵幀動畫。你指定動畫的一個圖層屬性的關(guān)鍵路徑,一個表示在動畫的每個階段的價(jià)值的數(shù)組,還有一個關(guān)鍵幀時(shí)間的數(shù)組和時(shí)間函數(shù)。
- CATransition提供了一個影響整個圖層的內(nèi)容過渡效果。在動畫顯示過程中采用淡出(fade)、推出(push)、顯露(reveal)圖層的內(nèi)容。 常用的過渡效果可以通過提供你自己定制的核心圖像濾鏡來擴(kuò)展。
除了要指定顯示的動畫類型,你還必須指定動畫的間隔、它的速度(它的插值如何分布在整個動畫過程)、動畫循環(huán)時(shí)候的循環(huán)次數(shù)、動畫周期完成的時(shí)候是否自動的反轉(zhuǎn)、還有動畫結(jié)束的時(shí)候它的可視化狀態(tài)。動畫類和CAMediaTiming協(xié)議提供所有這些功能甚至更多的功能。
CAAnimation、它的子類、時(shí)序協(xié)議被核心動畫和Cocoa Animation Proxy功能共享。這些類將會在“動畫類型和時(shí)序編程指南(Animation Types and Timing Programming Guide)”里面詳細(xì)介紹。
?
1.2 隱式動畫
核心動畫的隱式動畫模型假定所有動畫圖層屬性的變化應(yīng)該是漸進(jìn)的和異步的。動態(tài)的動畫場景可以在沒有顯式的動畫圖層時(shí)候?qū)崿F(xiàn)。改變可動畫顯示的圖層的屬性將會導(dǎo)致圖層隱式把圖層從舊的值動畫顯示為新的值。雖然動畫是持續(xù)的,但是設(shè)置新的目標(biāo)值時(shí)會導(dǎo)致圖層從當(dāng)前狀態(tài)動畫過渡到新的目標(biāo)值。
代碼1顯示了如果簡單的觸發(fā)一個隱式的動畫,把圖層從當(dāng)前位置動畫改變到新的位置。
代碼 1??隱式的動畫改變圖層的position屬性
//假設(shè)layer當(dāng)前position為(100.0,100.0) theLayer.position=CGPointMake(500.0,500.0);??
你可以隱式的一次動畫改變圖層的一個或者多個屬性。你還可以隱式的同時(shí)動畫改變多個圖層。代碼2的代碼實(shí)現(xiàn)了4個同時(shí)觸發(fā)的隱式動畫。
代碼 2??隱式的同時(shí)動畫改變多個圖層的多個屬性
// 在移動至遠(yuǎn)處時(shí)將Layer的opacity屬性漸進(jìn)至0 theLayer.opacity=0.0; theLayer.zPosition=-100;//在移動至近處時(shí)將Layer的opacity屬性漸進(jìn)至1 anotherLayer.opacity=1.0; anotherLayer.zPosition=100.0;???
隱式動畫使用動畫屬性中默認(rèn)指定的動畫時(shí)間,除非該時(shí)間已經(jīng)被隱式或者顯式的修改過。閱讀“重載覆蓋隱式動畫時(shí)間”獲取更多詳情。
?
1.3 顯式動畫
核心動畫同時(shí)提供了一個顯式的動畫模型。該顯式動畫模型需要你創(chuàng)建一個動畫對象,并設(shè)置開始和結(jié)束的值。顯式動畫不會開始執(zhí)行,直到你把該動畫應(yīng)用到某個圖層上面。代碼3中的代碼片段創(chuàng)建了一個顯式動畫,它實(shí)現(xiàn)一個層的不透明度從完全不透明過渡到完全透明的,3秒后返回重新執(zhí)行。動畫沒有開始,直到它被添加到某一圖層層。
代碼 3??顯式動畫
CABasicAnimation *theAnimation;theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; theAnimation.duration=3.0; theAnimation.repeatCount=2; theAnimation.autoreverses=YES; theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; theAnimation.toValue=[NSNumber numberWithFloat:0.0]; [theLayer addAnimation:theAnimation forKey:@"animateOpacity"];??
顯式動畫對于創(chuàng)建連續(xù)執(zhí)行的動畫非常有幫助。代碼4顯示了如何創(chuàng)建一個顯式動畫,把一個CoreImage濾鏡應(yīng)用到圖層上面,動畫顯示其強(qiáng)度。這將導(dǎo)致“選擇的圖層”跳動,吸引用戶的注意力。
代碼 4??連續(xù)顯式動畫示例
// The selection layer will pulse continuously. // This is accomplished by setting a bloom filter(夢維:用5.0和5.1模擬器測試發(fā)現(xiàn)CIBloom這個名稱無法初始化濾鏡,返回值為nil) on the layer // create the filter and set its default values CIFilter *filter = [CIFilter filterWithName:@"CIBloom"]; [filter setDefaults]; [filter setValue:[NSNumber numberWithFloat:5.0] forKey:@"inputRadius"];// name the filter so we can use the keypath to animate the inputIntensity // attribute of the filter [filter setName:@"pulseFilter"];// set the filter to the selection layer's filters [selectionLayer setFilters:[NSArray arrayWithObject:filter]];// create the animation that will handle the pulsing. CABasicAnimation* pulseAnimation = [CABasicAnimation animation];// the attribute we want to animate is the inputIntensity // of the pulseFilter pulseAnimation.keyPath = @"filters.pulseFilter.inputIntensity";// we want it to animate from the value 0 to 1 pulseAnimation.fromValue = [NSNumber numberWithFloat: 0.0]; pulseAnimation.toValue = [NSNumber numberWithFloat: 1.5];// over a one second duration, and run an infinite // number of times pulseAnimation.duration = 1.0; pulseAnimation.repeatCount = HUGE_VALF;// we want it to fade on, and fade off, so it needs to // automatically autoreverse.. this causes the intensity // input to go from 0 to 1 to 0 pulseAnimation.autoreverses = YES;// use a timing curve of easy in, easy out.. pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];// add the animation to the selection layer. This causes // it to begin animating. We'll use pulseAnimation as the // animation key name [selectionLayer addAnimation:pulseAnimation forKey:@"pulseAnimation"];??
1.4 開始和結(jié)束顯式動畫
你可以發(fā)送addAnimation:forKey:消息給目標(biāo)圖層來開始一個顯式動畫,把動畫和標(biāo)識符作為參數(shù)。一旦把動畫添加到目標(biāo)圖層,動畫將會一直執(zhí)行直到動畫完成,或者動畫被從圖層上面移除。把動畫添加到圖層時(shí)添加的標(biāo)識符,同樣也可以在停止動畫的時(shí)候使用,通過調(diào)用removeAnimationForKey:。你可以通過給圖層發(fā)送一個removeAllAnimations消息來停止圖層所有的動畫。
轉(zhuǎn)載于:https://www.cnblogs.com/kiao295338444/articles/2607372.html
總結(jié)
以上是生活随笔為你收集整理的CoreAnimation编程指南(六)动画 转自:http://www.dreamingwish.com/的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TypeForwardedTo Attr
- 下一篇: bridge模式