使图像遵循iPhone中的预定义路径? [英] Making an image follow a pre defined path in iPhone?

查看:53
本文介绍了使图像遵循iPhone中的预定义路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来使图像(球)沿着iPhone中的预定义路径移动.我的意图是创造一个类似于迷宫运动的球形运动.我知道有一种方法可以使用CGPath以编程方式创建路径.但是我相信很难创建复杂的路径. 是否有更好,更轻松的方法来从图像(看起来或代表路径)创建路径,并使图像(球)运动受限于该路径?

I am looking for some way to make an image (a ball) move along a predefined path in iPhone. My intention is to create a ball movement similar to the labyrinth movement. I understand there is a way to create paths programmatically using CGPath. But I believe it is difficult to create complex paths. Is there a better and easier way to create a path out of an image (which will look or represent a path) and make the image (ball) movement constrained in this path ?

在此先感谢您的帮助...

Thanx in advance for help ...

推荐答案

创建用于沿路径移动对象的动画并不难.例如,以下代码将沿特定的Bezier曲线进行动画处理:

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"];

该代码的中心部分是定义路径的位置.在这种情况下,我从currentPosition开始绘制,然后添加一条以endPoint结尾的曲线.该曲线的控制点为(endPoint.x, currentPosition.y)(endPoint.x, currentPosition.y).

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).

以这种方式定义矢量曲线,让Core Animation为您处理所有补间,比自己管理所有动画要容易得多.

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.

这篇关于使图像遵循iPhone中的预定义路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆