2

iPhoneで事前定義されたパスに沿って画像(ボール)を移動させる方法を探しています。私の意図は、迷路の動きに似たボールの動きを作成することです。CGPathを使用してプログラムでパスを作成する方法があることを理解しています。しかし、複雑なパスを作成するのは難しいと思います。画像からパスを作成し(パスを表示または表す)、このパスで画像(ボール)の動きを制限するためのより良い簡単な方法はありますか?

助けてくれてありがとう...

4

2 に答える 2

4

It's not really that hard to create an animation for moving an object along a path. For example, the following code will animate along a specific Bezier curve:

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 1.0f;
pathAnimation.calculationMode = kCAAnimationPaced;

CGPoint currentPosition = viewToAnimate.layer.position;
CGPoint endPoint = CGPointMake(currentPosition.x + 100.0f, currentPosition.y - 50.0f);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, currentPosition.x, currentPosition.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, currentPosition.y, endPoint.x, currentPosition.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);

pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
[viewToAnimate.layer addAnimation:pathAnimation forKey:@"animateMovementUsingPath"];

The center section of that code is where the path is defined. In this case, I start drawing at currentPosition, then add a curve which ends at endPoint. The control points for this curve are (endPoint.x, currentPosition.y) and (endPoint.x, currentPosition.y).

It will be far easier to define a vector curve in this fashion and let Core Animation handle all the tweening for you than to manage all of the animation yourself.

于 2010-08-25T15:08:20.473 に答える
0

実際の物理エンジンを考えたことはありますか? たとえば、弾丸はかなり素晴らしいです。

于 2010-08-24T18:24:44.307 に答える