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 回答