先在自定義的ViewController里聲明定義一個UIImageView
1 @property (nonatomic,retain) UIImageView *imgView;
1 @synthesize imgView;
在viewDidLoad函數里添加圖片,並執行組合動畫
1 //添加圖片
2 imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Icon@2x.png"]];
3 imgView.frame = CGRectMake(100, 100, imgView.frame.size.width, imgView.frame.size.height);
4 [self.view addSubview:imgView];
5 [imgView release];
6
7 //貝塞爾曲線路徑
8 UIBezierPath *movePath = [UIBezierPath bezierPath];
9 [movePath moveToPoint:CGPointMake(10.0, 10.0)];
10 [movePath addQuadCurveToPoint:CGPointMake(100, 300) controlPoint:CGPointMake(300, 100)];
11
12 //以下必須導入QuartzCore包
13 //關鍵幀動畫(位置)
14 CAKeyframeAnimation * posAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
15 posAnim.path = movePath.CGPath;
16 posAnim.removedOnCompletion = YES;
17
18 //縮放動畫
19 CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
20 scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
21 scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
22 scaleAnim.removedOnCompletion = YES;
23
24 //透明動畫
25 CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
26 opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
27 opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
28 opacityAnim.removedOnCompletion = YES;
29
30 //動畫組
31 CAAnimationGroup *animGroup = [CAAnimationGroup animation];
32 animGroup.animations = [NSArray arrayWithObjects:posAnim, scaleAnim, opacityAnim, nil];
33 animGroup.duration = 1;
34
35 [imgView.layer addAnimation:animGroup forKey:nil];