為了提升日後交付給客戶的應用體驗,我花了幾天研究 OC 的 CAAnimation 動畫類別,並動手做了幾個小案例。本文就以其中幾個案例作為主要內容來說明。
一、繼承結構

上圖中使用頻率最高的莫過於 CABaseAnimation 與 CAKeyframeAnimation。兩者最大的差別在於對動畫的控制:CABaseAnimation 無法在類別中增減或控制影格;而 CAKeyframeAnimation 可以在程式碼中增減影格動畫,並控制每一影格的執行時間。CAAnimation 是所有動畫類別的基底類別,因此它能實現其他類別的所有功能,我的案例中也用到了它。CAAnimationGroup 則是組合動畫,可以把其他五種動畫類別的動畫放入它的屬性陣列,再添加到目標視圖的 Layer 上,一次實現多種動畫效果。
二、案例
這裡一共貼兩個動畫案例:第一個是 CAAnimation,第二個是 CAAnimationGroup 組合動畫,其中就包含了 CABaseAnimation 與 CAKeyframeAnimation。
第一個 CAAnimation 動畫是為抽獎轉盤而寫的:中獎後從底部彈出提示視圖,最後一影主要實現對彈出視圖的隱藏,X 座標=設備寬度、Y 座標=0。
1:封裝動畫方法

- (CAAnimation*)Animation:(CGFloat)axisX jumpValue:(CGFloat)value thisType:(int)type {
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, axisX, DEVICE_Height);
if (type == 0) {
CGPathAddLineToPoint(path, NULL, axisX, 283);
} else if (type == 1) {
CGPathAddLineToPoint(path, NULL, axisX, 170);
} else if (type == 2) {
CGPathAddLineToPoint(path, NULL, axisX, 200);
} else {
CGPathAddLineToPoint(path, NULL, axisX, 283);
}
CGPathAddLineToPoint(path, NULL, axisX, 179);
CGPathAddLineToPoint(path, NULL, axisX, 207);
CGPathAddLineToPoint(path, NULL, axisX, 187);
CGPathAddLineToPoint(path, NULL, axisX, 199);
CGPathAddLineToPoint(path, NULL, axisX, 193);
CGPathAddLineToPoint(path, NULL, axisX, 195);
CGPathAddLineToPoint(path, NULL, DEVICE_Width, 0);
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
[animation setPath:path];
[animation setDuration:1.5];
[animation setCalculationMode:kCAAnimationLinear];
NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.12], [NSNumber numberWithFloat:0.24], [NSNumber numberWithFloat:0.36], [NSNumber numberWithFloat:0.48], [NSNumber numberWithFloat:0.60], [NSNumber numberWithFloat:0.72], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:value], nil];
[animation setKeyTimes:arr];
[animation setTimingFunctions:[NSArray arrayWithObjects:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], nil]];
animation.delegate = self;
animation.duration = 1.5;
CFRelease(path);
return animation;
}
2:呼叫

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
coinViewArray = [[NSMutableArray alloc] init]; // 這個陣列是一個成員,用來保存底部彈出的視圖,動畫結束後需要將它們移除
CGFloat width = DEVICE_Width - 60;
CGFloat everyWidht = (width - 60) / 4;
CGFloat theAxisX = 30;
for (int i = 0; i < 4; i++) {
UIImageView *scView = [[UIImageView alloc] initWithFrame:CGRectMake(theAxisX + i * 20 + i * everyWidht, DEVICE_Height, everyWidht, everyWidht)];
scView.image = [UIImage imageNamed:@"Coin.jpg"];
scView.layer.cornerRadius = everyWidht / 2;
scView.backgroundColor = [UIColor redColor];
[coinViewArray addObject:scView];
CGFloat jumpValue;
if (i == 0) {
jumpValue = 1.2;
} else if (i == 1) {
jumpValue = 1.1;
scView.backgroundColor = [UIColor yellowColor];
} else if (i == 2) {
scView.backgroundColor = [UIColor blueColor];
jumpValue = 1;
} else {
scView.backgroundColor = [UIColor greenColor];
jumpValue = 0.9;
}
[scView.layer addAnimation:[self Animation:scView.layer.position.x jumpValue:jumpValue thisType:i] forKey:@"position"];
[scView.layer setPosition:CGPointMake(scView.frame.origin.x, scView.frame.origin.y)];
[self addSubview:scView];
}
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LuckyCenterButton"]];
imgView.backgroundColor = [UIColor redColor];
imgView.frame = CGRectMake(100, 100, 50, 50);
[self addSubview:imgView];
}
3:代理(動畫結束後移除保存在陣列中的視圖)

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
for (int i = 0; i < coinViewArray.count; i++) {
UIImageView *imageView = [coinViewArray objectAtIndex:i];
[imageView removeFromSuperview];
}
}
CAAnimationGroup 主動畫:以貝塞爾曲線設定關鍵影的路徑,中間同時實現縮放、透明等效果。
1:程式碼

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LuckyCenterButton"]];
imgView.backgroundColor = [UIColor redColor];
imgView.frame = CGRectMake(100, 100, 50, 50);
[self addSubview:imgView];
// 貝塞爾曲線路徑
UIBezierPath *movePath = [UIBezierPath bezierPath];
// 設置動畫的起點座標
[movePath moveToPoint:CGPointMake(DEVICE_Width/2-25, DEVICE_Height/2-25)];
// CABasicAnimation 可看做是最多只有 2 個關鍵影的 CAKeyframeAnimation
[movePath addQuadCurveToPoint:CGPointMake(DEVICE_Width, 0) controlPoint:self.view.center];
// 以下必須導入 QuartzCore 包
// 關鍵影動畫(位置)
CAKeyframeAnimation *posAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
posAnim.path = movePath.CGPath;
posAnim.removedOnCompletion = YES;
// 縮放動畫
CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.3, 0.3, 1)];
scaleAnim.removedOnCompletion = YES;
// 透明動畫
CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
opacityAnim.removedOnCompletion = YES;
// 動畫組
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:posAnim, scaleAnim, opacityAnim, nil];
animGroup.duration = 10;
// 需要對動畫的兩個屬性進行設置,讓圖層在完成動畫後停留在動畫後的狀態
animGroup.fillMode = kCAFillModeForwards;
animGroup.removedOnCompletion = NO;
[imgView.layer addAnimation:animGroup forKey:nil];
}