[编辑]请注意我还必须将UIImageView作为子视图添加到当前视图。
最后用我以前给的例子搞清楚了。这里是我的步骤:
创建路径。 (注意P = CGPointMake在下面的代码)
UIBezierPath *trackPath = [UIBezierPath bezierPath];
[trackPath moveToPoint:P(160, 25)];
[trackPath addCurveToPoint:P(300, 120)
controlPoint1:P(320, 0)
controlPoint2:P(300, 80)];
[trackPath addCurveToPoint:P(80, 380)
controlPoint1:P(300, 200)
controlPoint2:P(200, 480)];
....
创建的UIImageView,并给它的动画图像的阵列。
UIImageView *test = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[test setAnimationImages:[NSArray arrayWithObjects:[UIImage imageNamed:@"bee-1"],
[UIImage imageNamed:@"bee-2"], nil]];
[test startAnimating];
设置的UIImageView的层位置和层添加到视图中的层:
[test.layer setPosition:P(160,25)];
[self.view.layer addSublayer:test.layer];
创建CAKeyframeAnimation:
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.path = trackPath.CGPath;
anim.rotationMode = kCAAnimationRotateAuto;
anim.repeatCount = HUGE_VALF;
anim.duration = 8.0;
[test.layer addAnimation:anim forKey:@"race"];