我們首先來看看Core Animation類的繼承關(guān)系圖
687474703a2f2f696d672e6d792e6373646e2e6e65742f75706c6f6164732f3230313530372f32332f313433373631373536325f333139302e706e67.png
下面通過蘋果官方的API來介紹CAKeyframeAnimation
CAKeyframeAnimation
An object that provides keyframe animation capabilities for a layer object.
//為圖層對象提供關(guān)鍵幀動畫功能的對象。
Overview
You create a CAKeyframeAnimation object using the inherited animationWithKeyPath: method, specifying the key path of the property that you want to animate on the layer. You can then specify the keyframe values to use to control the timing and animation behavior.
//使用繼承的animationWithKeyPath:方法創(chuàng)建CAKeyframeAnimation對象绪商,指定要在圖層上進行動畫處理的屬性的鍵路徑苛谷。 然后,您可以指定要用于控制時序和動畫行為的關(guān)鍵幀值格郁。
For most types of animations, you specify the keyframe values using the values and keyTimes properties. During the animation, Core Animation generates intermediate values by interpolating between the values you provide. When animating a value that is a coordinate point, such as the layer’s position, you can specify a path for that point to follow instead of individual values. The pacing of the animation is controlled by the timing information you provide.
//對于大多數(shù)類型的動畫腹殿,您可以使用values和keyTimes屬性指定關(guān)鍵幀值独悴。 在動畫期間,Core Animation通過在您提供的值之間插值來生成中間值锣尉。 當(dāng)作為坐標(biāo)點的值(例如圖層的位置)進行動畫處理時刻炒,可以指定該點的路徑而不是單個值。 動畫的步調(diào)由您提供的時間信息控制自沧。
Providing keyframe values:
values(指定要用于動畫的關(guān)鍵幀值的對象數(shù)組落蝙。)
An array of objects that specify the keyframe values to use for the animation.
path(基于點的屬性的路徑)
The path for a point-based property to follow.
Keyframe timing:
keyTimes(NSNumber對象的可選數(shù)組,用于定義應(yīng)用給定關(guān)鍵幀段的時間)
An optional array of NSNumber objects that define the time at which to apply a given keyframe segment.
timingFunctions(CAMediaTimingFunction對象的可選數(shù)組暂幼,用于定義每個關(guān)鍵幀段的步調(diào)。)
An optional array of CAMediaTimingFunction objects that define the pacing for each keyframe segment.
calculationMode(指定接收器如何計算中間關(guān)鍵幀值移迫。)
Specifies how intermediate keyframe values are calculated by the receiver.
Rotation Mode Attribute:
rotationMode(確定沿路徑動畫的對象是否旋轉(zhuǎn)以匹配路徑切線旺嬉。)
Determines whether objects animating along the path rotate to match the path tangent.
Cubic Mode Attributes:
tensionValues(定義曲線緊密度的NSNumber對象數(shù)組)
An array of NSNumber objects that define the tightness of the curve.
continuityValues(NSNumber對象的數(shù)組,定義了時序曲線拐角的銳度)
An array of NSNumber objects that define the sharpness of the timing curve’s corners.
biasValues(一個NSNumber對象數(shù)組厨埋,用于定義曲線相對于控制點的位置)
An array of NSNumber objects that define the position of the curve relative to a control point.
Constants:
Rotation Mode Values(這些常量由rotationMode屬性使用邪媳。)
These constants are used by the rotationMode property.
Value calculation modes(這些常量由calculateMode屬性使用)
These constants are used by the calculationMode property.
CAKeyframeAnimation
- CAKeyframeAnimation和CABaseAnimation都屬于CAPropertyAnimatin的子類,CABaseAnimation只能從一個數(shù)值(fromValue)變換成另一個數(shù)值(toValue),而CAKeyframeAnimation則會使用一個NSArray保存一組關(guān)鍵幀.
重要屬性
- values : 一個NSArray對象。里面的元素稱為”關(guān)鍵幀”(keyframe),動畫對象會在指定的時間(duration)內(nèi)荡陷,依次顯示values數(shù)組中的每一個關(guān)鍵幀
- path : (畫圓雨效、橢圓、貝塞兒曲線)可以設(shè)置一個CGPathRef\CGMutablePathRef,讓層跟著路徑移動,path只對CALayer的anchorPoint和position起作用,如果你設(shè)置了path废赞,那么values將被忽略
- keyTimes : 可以為對應(yīng)的關(guān)鍵幀指定對應(yīng)的時間點,其取值范圍為0到1.0,keyTimes中的每一個時間值都對應(yīng)values中的每一幀.當(dāng)keyTimes沒有設(shè)置的時候,各個關(guān)鍵幀的時間是平分的
- timingFunctions: 控制動畫快進慢出徽龟、慢進快出等特性
- rotationMode(確定沿路徑動畫的對象是否旋轉(zhuǎn)以匹配路徑切線)
- tensionValues(定義曲線緊密度的NSNumber對象數(shù)組)
- continuityValues(NSNumber對象的數(shù)組,定義了時序曲線拐角的銳度)
- biasValues(一個NSNumber對象數(shù)組唉地,用于定義曲線相對于控制點的位置)
- Rotation Mode Values(這些常量由rotationMode屬性使用据悔。)
- Value calculation modes(這些常量由calculateMode屬性使用)
由API我們知道我們需要使用animationWithKeyPath:方法來創(chuàng)建一個CAKeyframeAnimation(關(guān)鍵幀)對象,指定要在圖層上進行動畫處理的屬性的鍵路徑,通過設(shè)置values和keyTimes屬性來指定關(guān)鍵幀值耘沼,例如:
//創(chuàng)建動畫并指定動畫鍵路徑
CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//通過values來設(shè)置關(guān)鍵幀值
NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-50)];
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2-50)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2+50)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2+50)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2-50)];
NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-50)];
anima.values = [NSArray arrayWithObjects:value0,value1,value2,value3,value4,value5, nil];
anima.duration = 2.0f;
anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];//設(shè)置動畫的節(jié)奏
anima.delegate = self;//設(shè)置代理极颓,可以檢測動畫的開始和結(jié)束
[_demoView.layer addAnimation:anima forKey:@"keyFrameAnimation"];
使用path
CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//這個方法根據(jù)傳入的rect矩形參數(shù)繪制一個內(nèi)切曲線。
//當(dāng)傳入的rect是一個正方形時群嗤,繪制的圖像是一個內(nèi)切圓菠隆;
//當(dāng)傳入的rect是一個長方形時,繪制的圖像是一個內(nèi)切橢圓狂秘。
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(SCREEN_WIDTH/2-100, SCREEN_HEIGHT/2-100, 200, 200)];
anima.path = path.CGPath;
anima.duration = 2.0f;
[_demoView.layer addAnimation:anima forKey:@"pathAnimation"];
你可以指定一個values數(shù)組來指定動畫的執(zhí)行過程(路徑)骇径,也可以通過設(shè)置path來指定動畫的執(zhí)行過程(路徑)