Core Animation基礎介紹、簡單使用CALayer以及多種動畫效果

Core Animation之基礎介紹 html

 Core Animation能夠翻譯爲核心動畫,它爲圖形渲染和動畫 ios

提供了基礎。使用核心動畫,你只須要設置一些參數好比起點和終點,剩下的幀核心動畫爲你自動完成。核心動畫使用硬件加速,不用消耗cpu資源。其實平時我們開發的iOS應用都在有意無心的使用了核心動畫。動畫不會替代View,而是和View一塊兒提供更好的性能。Core Animation經過緩存view上的內容到bitmap,這樣bitmap就能夠直接在圖形硬件上操做。從而提升了性能。 緩存

 
一、關於層類
Layer Classes是core animation的基礎。Layer Classes提供了一個抽象的概念,這個概念對於那些使用NSview和UIview的開發者來講是很熟悉的。基礎層是由CAlayer類提供的,CAlayer是全部Core Animation層的父類。    
同一個視圖類的實例同樣,一個CAlayer實例也有一個單獨的superlayer和上面全部的子層(sublayers),它建立了一個有層次結構的層,咱們稱之爲layer tree。layers的繪製就像views同樣是從後向前繪製的,繪製的時候咱們要指定其相對與他們的superlayer的集合形狀,同時還須要建立一個局部的座標系。layers能夠作一些更復雜的操做,例如rotate(旋轉),skew(傾斜),scale(放縮),和project the layer content(層的投影)。   
 
圖層的內容提供:
(1)直接設置層的content屬性到一個core graphics圖,或者經過delegation來設置
(2)提供一個代理直接繪製到Core Graphics image context(核心圖形的上下文)
(3)設置任意數量的全部層共有的可視的風格屬性。例如:backgroundColor(背景色),opacity(透明度)和masking(遮罩)。max os x應用經過使用core image filters來達到這種可視化的屬性。
(4)子類化CAlayer,同時在更多的封裝方式中完成上面的任意技術。 
 
   1.1 CALayer的子類和他們的使用場景

Class app

Usage ide

CAEmitterLayer 函數

Used to implement a Core Animation–based particle emitter system. The emitter layer object controls the generation of the particles and their origin. 性能

CAGradientLayer 動畫

Used to draw a color gradient that fills the shape of the layer (within the bounds of any rounded corners). spa

CAEAGLLayer/CAOpenGLLayer 翻譯

Used to set up the backing store and context needed to draw using OpenGL ES (iOS) or OpenGL (OS X).

CAReplicatorLayer

Used when you want to make copies of one or more sublayers automatically. The replicator makes the copies for you and uses the properties you specify to alter the appearance or attributes of the copies.

CAScrollLayer

Used to manage a large scrollable area composed of multiple sublayers.

CAShapeLayer

Used to draw a cubic Bezier spline. Shape layers are advantageous for drawing path-based shapes because they always result in a crisp path, as opposed to a path you draw into a layer’s backing store, which would not look as good when scaled. However, the crisp results do involve rendering the shape on the main thread and caching the results.

CATextLayer

Used to render a plain or attributed string of text.

CATiledLayer

Used to manage a large image that can be divided into smaller tiles and rendered individually with support for zooming in and out of the content.

CATransformLayer

Used to render a true 3D layer hierarchy, rather than the flattened layer hierarchy implemented by other layer classes.

QCCompositionLayer

Used to render a Quartz Composer composition. (OS X only)

1.二、 anchorPoint、 position
anchorPoint又稱錨點,錨點對動畫是有很大影響的。下圖描述了基於錨點的三個示例值:
 
1.三、 圖層的 frame、bounds、position 和 anchorPoint 關係以下圖所示: 
在該示例中,anchorPoint 默認值爲(0.5,0.5),位於圖層的中心點。圖層的 position 值爲(100.0,100.0),bounds 爲(0.0,0.0,120,80.0)。經過計算獲得圖層的 frame爲(40.0,60.0,120.0,80.0)。
 
若是你新建立一個圖層,則只有設置圖層的 frame 爲(40.0,60.0,120.0,80.0),相應的 position 屬性值將會自動設置爲(100.0,100.0),而 bounds 會自動設置爲 (0.0,0.0,120.0,80.0)。下圖顯示一個圖層具備相同的 frame(如上圖),可是在該圖中它的 anchorPoint 屬性值被設置爲(0.0,0.0),位於圖層的左下角位置。
圖層的 frame 值一樣爲(40.0,60.0,120.0,80.0),bounds 的值不變,可是圖層的 position 值已經改變爲(40.0,60.0)。
 
二、關於動畫類
核心動畫的動畫類使用基本的動畫和關鍵幀動畫把圖層的內容和選取的屬性動畫的顯示出來。全部核心動畫的動畫類都是從 CAAnimation 類繼承而來。
 
CAAnimation 實現了 CAMediaTiming 協議,提供了動畫的持續時間,速度,和重複計數。 CAAnimation 也實現了 CAAction 協議。該協議爲圖層觸發一個動畫動做提供了提供標準化響應。動畫類同時定義了一個使用貝塞爾曲線來描述動畫改變的時間函數。例如,一個 勻速時間函數(linear timing function)在動畫的整個生命週期裏面一直保持速度不變, 而漸緩時間函數(ease-out timing function)則在動畫接近其生命週期的時候減慢速度。核心動畫額外提供了一系列抽象的和細化的動畫類,好比:CATransition 提供了一個圖層變化的過渡效果,它能影響圖層的整個內容。 動畫進行的時候淡入淡出(fade)、推(push)、顯露(reveal)圖層的內容。這些過渡效 果能夠擴展到你本身定製的 Core Image 濾鏡。CAAnimationGroup 容許一系列動畫效果組合在一塊兒,並行顯示動畫。
 
2.1動畫類
 CAPropertyAnimation :是一個抽象的子類,它支持動畫的顯示圖層的關鍵路 徑中指定的屬性通常不直接使用,而是使用它的子類,CABasicAnimation,CAKeyframeAnimation. 在它的子類裏修改屬性來運行動畫。
CABasicAnimation: 簡單的爲圖層的屬性提供修改。 不少圖層的屬性修改默認會執行這個動畫類。好比大小,透明度,顏色等屬性
 CAKeyframeAnimation: 支持關鍵幀動畫,你能夠指定的圖層屬性的關鍵路徑動畫,包括動畫的每一個階段的價值,以及關鍵幀時間和計時功能的一系列值。在 動畫運行是,每一個值被特定的插入值替代。核心動畫 和 Cocoa Animation 同時使用這些動畫類。
 
2.2 如何使用多個動畫效果疊加
在執行動畫的過程當中須要同時修改position,alpha, frame等屬性,使用CAAnimationGroup能夠將三個動畫合成一塊兒執行:
 
//http://www.baisoujs.com/list_wp7_wp7article.html 

 
 
 
 

 

 

  
  
  
  
  

 
  1. CAAnimationGroup *animGroup = [CAAnimationGroup animation];  
  2. animGroup.animations = [NSArray arrayWithObjects:moveAnim,scaleAnim,opacityAnim, nil]; 
  3. animGroup.duration = 1; 
  4. [view.layer addAnimation:animGroup forKey:nil]; 
 
2.3事務管理類  
   圖層的動畫屬性的每個修改必然是事務的一個部分。CATransaction 是核心動畫裏面負責協調多個動畫原子更新顯示操做。事務支持嵌套使用。 
 
2.4 Core Animation類的繼承關係圖 

更多詳情請參考原文:http://www.baisoujs.com/detail_137290948705248.html

相關文章
相關標籤/搜索