[編輯]請注意我還必須將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"];