core Animation總結

核心動畫的基本結構數組

CAAnimation 是個虛類 不能直接使用 繼承之下有三個子類 CAAnimationGroup CATransition CAPropertyAnimation 和 一個協議 CAMediaTimingxcode

其中CAPropertyAnimation 又是一個虛類一樣不能直接使用 繼承之下又有兩個子類 也是經常使用類 CABasicAnimation 和 CAKeyframeAnimation併發

若是是xcode6 以前 在用 核心動畫的時候要先導入<QuartzCore/QuartzCore.h>框架框架

基本原理函數

coreAnimation 都是做用在 CALayer 上的。CALayer 只是一個圖層不具有事件響應,只負責圖層的渲染展現。與view相似 有兩個方法動畫

1 添加動畫到CALayer addAnimation:forKey: xxxurl

2 移除動畫 removeAnimationForKey: xxx代理

以本身寫的一個登錄動畫爲實例來具體總結一下:Demo地址 (求點個 star)code

1 CABasicAnimation 基本動畫orm

登陸按鈕環繞實例 按鈕變圓 登陸實拍顏色變紅 都用的是基本動畫不在一一列舉

CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; // key 肯定用什麼形式動畫

rotate.fromValue = 0; //初始值

rotate.toValue = @(M_PI * 2);//最終值 keypath 不同表明的意義也不同

rotate.duration = 0.4;//動畫持續時間

rotate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];//動畫節奏 時間線性函數

rotate.repeatCount = NSNotFound;//重複次數 NSNotFound MAXFLOAT 爲無限次

rotate.fillMode = kCAFillModeForwards;//動畫結束後的狀態

rotate.removedOnCompletion = false;//結束後是否移除

[layer addAnimation:rotate forKey:rotate.keyPath]; //在layer 上添加動畫 若是是view 則view.layer 首先是keyPath key 就有不少種形式:

transform.scale = 比例轉換

transform.scale.x = 寬的比例轉換

transform.scale.y = 高的比例轉換

transform.rotation.z = 平面圖的旋轉

opacity = 透明度

margin=邊框間隔?

zPosition = 平面圖的位置

backgroundColor = 背景色

cornerRadius = layer的角度

borderWidth = 邊框寬度

contents = 內容?

bounds = 大小?

contentsRect = 內容矩形

frame = 位置

hidden = 隱藏

mask = 標記

maskToBounds

position = 位置

shadowOffset = 陰影偏移?

shadowColor = 陰影顏色

shadowRadius = 陰影角度

fromValue 和 toValue 一個是初值一個是終值 根據不一樣的keypath 所填的值有所不一樣,意義也就不同。

timingFunction 動畫節奏 時間函數:

//kCAMediaTimingFunctionLinear 線性勻速

//kCAMediaTimingFunctionEaseIn 先慢後快

//kCAMediaTimingFunctionEaseOut 先快後慢

//kCAMediaTimingFunctionEaseInEaseOut 中間快兩頭慢

//kCAMediaTimingFunctionDefault 通常的動畫

fillMode動畫結束後的狀態類型:

// kCAFillModeRemoved 這個是默認值,也就是說當動畫開始前和動畫結束後,動畫對layer都沒有影響,動畫結束後,layer會恢復到以前的狀態

// kCAFillModeForwards 當動畫結束後,layer會一直保持着動畫最後的狀

// kCAFillModeBackwards 在動畫開始前,只須要將動畫加入了一個layer,layer便當即進入動畫的初始狀態並等待動畫開始。

// kCAFillModeBoth 這個其實就是上面兩個的合成.動畫加入後開始以前,layer便處於動畫初始狀態,動畫結束後layer保持動畫最後的狀態

removedOnCompletion 結束後是否移除

最後記得把 動畫添加在相應的layer上

2 CAKeyframeAnimation——關鍵幀動畫

登陸按鈕 登陸失敗 左右晃動的實例

CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

CGPoint point = self.layer.position;

keyFrameAnimation.values = @[[NSValue valueWithCGPoint:CGPointMake(point.x, point.y)],[NSValue valueWithCGPoint:CGPointMake(point.x - 10, point.y)],[NSValue valueWithCGPoint:CGPointMake(point.x + 10, point.y)],[NSValue valueWithCGPoint:CGPointMake(point.x - 10, point.y)],[NSValue valueWithCGPoint:CGPointMake(point.x + 10, point.y)],[NSValue valueWithCGPoint:CGPointMake(point.x - 10, point.y)],[NSValue valueWithCGPoint:CGPointMake(point.x + 10, point.y)],[NSValue valueWithCGPoint:CGPointMake(point.x, point.y)]];

keyFrameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

keyFrameAnimation.duration = 0.5f;

keyFrameAnimation.delegate = self ;

self.layer.position = point;

[self.layer addAnimation:keyFrameAnimation forKey:keyFrameAnimation.keyPath]; 關鍵幀動畫 是CABasicAnimation動畫的升級版 CABasicAnimation能夠看作是它的特例 即 只有兩個關鍵幀。區別是:

CABasicAnimation只能從一個數值(fromValue)變到另外一個數值(toValue),而CAKeyframeAnimation會使用一個NSArray保存這些數值

屬性說明:

values:上述的NSArray對象。裏面的元素稱爲「關鍵幀」(keyframe)。動畫對象會在指定的時間(duration)內,依次顯示values數組中的每個關鍵幀

path:能夠設置一個CGPathRef、CGMutablePathRef,讓圖層按照路徑軌跡移動。path只對CALayer的anchorPoint和position起做用。若是設置了path,那麼values將被忽略

keyTimes:能夠爲對應的關鍵幀指定對應的時間點,其取值範圍爲0到1.0,keyTimes中的每個時間值都對應values中的每一幀。若是沒有設置keyTimes,各個關鍵幀的時間是平分的

因此它能夠作一些位置的移動什麼的 主要還的看需求。

3 CAAnimationGroup——動畫組

動畫組,是CAAnimation的子類,能夠保存一組動畫對象,將CAAnimationGroup對象加入層後,組中全部動畫對象能夠同時併發運行。

因此當須要在一個圖層上集成多個動畫的話就能夠用到動畫組。

屬性說明:

animations:用來保存一組動畫對象的NSArray

默認狀況下,一組動畫對象是同時運行的,也能夠經過設置動畫對象的beginTime屬性來更改動畫的開始時間,來達到必定的效果。

CATransition——轉場動畫

CATransition是CAAnimation的子類,用於作轉場動畫,可以爲層提供移出屏幕和移入屏幕的動畫效果。

UINavigationController就是經過CATransition實現了將控制器的視圖推入屏幕的動畫效果

動畫屬性:

type:動畫過渡類型

subtype:動畫過渡方向

startProgress:動畫起點(在總體動畫的百分比)

endProgress:動畫終點(在總體動畫的百分比)

  1. 單視圖
  • (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;

參數說明:

duration:動畫的持續時間

view:須要進行轉場動畫的視圖

options:轉場動畫的類型

animations:將改變視圖屬性的代碼放在這個block中

completion:動畫結束後,會自動調用這個block

  1. 雙視圖
  • (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion;

參數說明:

duration:動畫的持續時間

options:轉場動畫的類型

animations:將改變視圖屬性的代碼放在這個block中

completion:動畫結束後,會自動調用這個block

  1. 自定義轉場(其實仍是屬於系統寫好的)

建立轉場動畫:[CATransition animation]

設置動畫屬性值

添加到須要轉場動畫的圖層上 [layer addAnimation:animation forKey:nil]

轉場動畫的類型(NSString *type)

fade : 交叉淡化過渡

push : 新視圖把舊視圖推出去

moveIn: 新視圖移到舊視圖上面

reveal: 將舊視圖移開,顯示下面的新視圖

cube : 立方體翻滾效果

oglFlip : 上下左右翻轉效果

suckEffect : 收縮效果,如一塊布被抽走

rippleEffect: 水滴效果

pageCurl : 向上翻頁效果

pageUnCurl : 向下翻頁效果

cameraIrisHollowOpen : 相機鏡頭打開效果

cameraIrisHollowClos : 相機鏡頭關閉效果

轉場動畫的方向(NSString *subtype)

從某個方向開始:fromLeft, fromRight, fromTop ,fromBottom

4 真正的自定義轉場

王巍的轉場動畫講解 在這裏是王魏對iOS7後出現的自定義轉場動畫作比較詳細的講解,

若是你想要進一步的研究能夠進去看看相信能夠學到不少。//關於轉場的真正操做只須要遵照協議 UIViewControllerAnimatedTransitioning,CAAnimationDelegate

//實現兩個方法:

1 - (NSTimeInterval)transitionDuration:(id)transitionContext 在這裏設定轉場所用時間// 2 - (void)animateTransition:(id)transitionContext 具體的轉場操做在這而函數中實現就能夠了

CoreAnimation中的CALayer

  1. CAReplicatorLayer:複製圖層

顧名思義,複製圖層就是用來複制的。它會將本身的子圖層進行復制,連同子layer上的動畫會一塊兒複製。

屬性說明:

instanceCount :複製份數。(會把原始的子圖層複製多少份,包括原來的一份)

instanceTransform:形變。每一份相對上一份的形變量

instanceDelay :每一份相對上一份的時間延遲。

修改CAReplicatorLayer的顏色通道,能夠改變CAReplicatorLayer的顯示的樣式

@property float instanceRedOffset;

@property float instanceGreenOffset;

@property float instanceBlueOffset;

@property float instanceAlphaOffset;

  1. CAShapeLayer:形狀圖層

根據形狀,繪製內容的圖層

屬性說明:

path:描述形狀的路徑。默認會把路徑封閉,而後填充。

fillColor :填充的顏色

strokeColor:描邊的顏色(若是想僅僅只描邊的話,就把fillColor設爲clearcolor就好)

strokeStart:開始描邊的比例【0-1】

strokeEnd:結束描邊的比例【0-1】

  1. CAGradientLayer:漸變圖層

用這個圖層,能夠作顏色的漸變

屬性說明:

colors :保存全部漸變的顏色,裏面是CGColorRef,記得用id,讓編譯器覺得數組裏面的是OC對象。

locations :保存全部漸變的位置【0-1】

startPoint:開始漸變的點【0-1】

endPoint:結束漸變的點【0-1】

4 CAAnimation代理方法

CAAnimation在分類中定義了代理方法。是給NSObject添加的分類,因此任何對象,成爲CAAnimation的代理,均可以。

動畫開始的時候調用

  • (void)animationDidStart:(CAAnimation *)anim;

動畫中止的時候調用

  • (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;

5 CALayer上動畫的暫停和恢復

暫停

-(void)pauseLayer:(CALayer*)layer

{

CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];

// 讓CALayer的時間中止走動

layer.speed = 0.0;

// 讓CALayer的時間停留在pausedTime這個時刻

layer.timeOffset = pausedTime;

} 恢復

-(void)resumeLayer:(CALayer*)layer

{

CFTimeInterval pausedTime = layer.timeOffset;

// 1. 讓CALayer的時間繼續行走

layer.speed = 1.0;

// 2. 取消上次記錄的停留時刻

layer.timeOffset = 0.0;

// 3. 取消上次設置的時間

layer.beginTime = 0.0;

// 4. 計算暫停的時間(這裏也能夠用CACurrentMediaTime()-pausedTime)

CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;

// 5. 設置相對於父座標系的開始時間(日後退timeSincePause)

layer.beginTime = timeSincePause;

} 注意:

核心動畫給咱們展現的只是一個假象,layer的的frame、bounds、position並不會在動畫完畢以後發生改變。

UIView封裝的動畫,會使會真實修改view的一些屬性。

UIView 經常使用的分裝動畫:

1 基本動畫 通常用於簡單的只有一個動效的動畫

[UIView animateWithDuration:(NSTimeInterval) animations:^{

}] 2 升級版的能夠多層嵌套 完成稍微複雜點的動畫

[UIView animateWithDuration:(NSTimeInterval) animations:^{

在這裏寫要操做的動畫

} completion:^(BOOL finished) {

這裏進行動畫結束後的操做

}] 3 帶有彈簧動效的動畫

[UIView animateWithDuration:(NSTimeInterval) delay:(NSTimeInterval) // 動畫延遲時間 usingSpringWithDamping:0< =彈性係數 <=1 initialSpringVelocity: options:(UIViewAnimationOptions) animations:^{

} completion:^(BOOL finished) {

}] 本人本身平時積累研究動畫的時候寫了一個小的動畫庫,已經封裝好須要的接口。能夠直接脫出須要的動畫用。(特殊須要本身修改)藤王俊採 動畫Demo

相關文章
相關標籤/搜索