之所以叫做關鍵幀動畫是因為,這個類可以實現,某一屬性按照一串的數值進行動畫,就好像制作動畫的時候一幀一幀的制作一樣。
一般使用的時候 首先通過 animationWithKeyPath 方法 創建一個CAKeyframeAnimation實例,
CAKeyframeAnimation 的一些比較重要的屬性
1. path
這是一個 CGPathRef 對象,默認是空的,當我們創建好CAKeyframeAnimation的實例的時候,可以通過制定一個自己定義的path來讓 某一個物體按照這個路徑進行動畫。這個值默認是nil 當其被設定的時候 values 這個屬性就被覆蓋
2. values
一個數組,提供了一組關鍵幀的值, 當使用path的 時候 values的值自動被忽略。
下面是一個簡單的例子 效果為動畫的連續移動一個block到不同的位置
CGMutablePathRef path = CGPathCreateMutable();
//將路徑的起點定位到 (50 120)
CGPathMoveToPoint(path,NULL,50.0,120.0);
//下面5行添加5條直線的路徑到path中
CGPathAddLineToPoint(path, NULL, 60, 130);
CGPathAddLineToPoint(path, NULL, 70, 140);
CGPathAddLineToPoint(path, NULL, 80, 150);
CGPathAddLineToPoint(path, NULL, 90, 160);
CGPathAddLineToPoint(path, NULL, 100, 170);
//下面四行添加四條曲線路徑到path
CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);
CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,90.0,120.0);
CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,110.0,120.0);
CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,130.0,120.0);
//以“position”為關鍵字 創建 實例
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//設置path屬性
[animation setPath:path];
[animation setDuration:3.0];
//這句代碼 表示 是否動畫回到原位
// [animation setAutoreverses:YES];
CFRelease(path);
[self.block.layer addAnimation:animation forKey:NULL];
運行后 block會先沿着直線移動,之后再沿着設定的曲線移動,完全按照我們設定的“關鍵幀”移動。
下面一個例子是利用values制作的動畫
CGPoint p1=CGPointMake(50, 120);
CGPoint p2=CGPointMake(80, 170);
CGPoint p3=CGPointMake(30, 100);
CGPoint p4=CGPointMake(100, 190);
CGPoint p5=CGPointMake(200, 10);
NSArray *values=[NSArray arrayWithObjects:[NSValue valueWithCGPoint:p1],[NSValue valueWithCGPoint:p2],[NSValue valueWithCGPoint:p3],[NSValue valueWithCGPoint:p4],[NSValue valueWithCGPoint:p5], nil];
CAKeyframeAnimation
*animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; [animation setValues:values];
[animation setDuration:3.0];
[animation setAutoreverses:YES];
[self.block.layer addAnimation:animation forKey:NULL];
也非常簡單,到目前位置,只用到了CAKeyframeAnimation的兩個關鍵屬性就能完成動畫,下面的一些屬性提供了更加細致化,更加強大的功能。
設定每一幀的時間
默認情況下,一幀動畫的播放,分割 的時間是動畫的總時間除以幀數減去一。你可以通過下面的公式決定每幀動畫的時間:總時間/(總幀數-1)。 例如,如果你指定了一個 5 幀,10 秒的動畫,那么每幀的時間就是 2.5 秒鍾:10/(5-1)=2.5。你可以做更多 的控制通過使用 keyTimes 關鍵字,你可以給每幀動畫指定總時間之內的某個時間點。
通過設置屬性keyTimes,能實現這個功能,這個屬性是一個數組,其成員必須是NSNumber。
同時 這個屬性的設定值要與calculationMode屬性相結合,同時他們有一定的規則,
The appropriate values in the keyTimes array are dependent on the calculationMode property.
-
If the calculationMode is set to
kCAAnimationLinear, the first value in the array must be 0.0 and the last value must be 1.0. Values are interpolated between the specified key times. -
If the calculationMode is set to
kCAAnimationDiscrete, the first value in the array must be 0.0. -
If the calculationMode is set to
kCAAnimationPacedorkCAAnimationCubicPaced, thekeyTimesarray is ignored。
如果keyTimes的值不合法,或者不符合上面的規則,那么就會被忽略。
[animation setCalculationMode:kCAAnimationLinear]; [animation setKeyTimes: [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.25], [NSNumber numberWithFloat:0.50], [NSNumber numberWithFloat:0.75], [NSNumber numberWithFloat:1.0], nil]];
calculationMode
這個屬性用來設定 關鍵幀中間的值是怎么被計算的
可選的值有
NSString * const kCAAnimationLinear; NSString * const kCAAnimationDiscrete; 只展示關鍵幀的狀態,沒有中間過程,沒有動畫。 NSString * const kCAAnimationPaced; NSString * const kCAAnimationCubic; NSString * const kCAAnimationCubicPaced;
關鍵幀動畫的基礎步驟
1.決定你想要做動畫的屬性(例如,框架,背景,錨點,位置,邊框,等等) 2.在動畫對象值的區域中,指定開始,結束,和中間的值。這些都是你的關鍵幀(看清單 4-2)
3.使用 duration 這個字段指定動畫的時間
4.通常來講,通過使用 times 這個字段,來給每幀動畫指定一個時間。如果你沒有指定這些,核心動畫就
會通過你在 values 這個字段指定的值分割出時間段。
5.通常,指定時間功能來控制步調。 這些都是你需要做的。你創建你的動畫和增加他們到層中。調用-addAnimation 就開始了動畫。
