先在自定义的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];