iOS:移动 UIImageView

发布于 2024-12-09 02:46:18 字数 93 浏览 0 评论 0原文

在我的应用程序中,我想在 .png 中移动一些 UIImageView;这是一只小昆虫,我想模拟他的飞行。例如,我希望这个 png 在将倒八移动为无限符号 ∞ 时执行此操作

In my app I want to move a little UIImageView with inside a .png; this is a little insect and I want to simulate his flight. At example I want that this png do when it move an inverted eight as the infinite simbol ∞

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情出卖 2024-12-16 02:46:18

您可以使用 CoreAnimation。您可以对视图进行子类化,为昆虫创建子视图,然后按照定义的路径为其分配动画。

你的 UIImageView 可以是动画的。如果是苍蝇,您可以为翅膀移动做一些帧:

NSArray *images = [NSArray arrayWithObjects:..., nil];

insect.animationImages = images;
insect.animationDuration = ??;
insect.animationRepeatCount = 0;
[insect startAnimating];

然后为昆虫设置一个初始帧:

insect.frame = CGRectMake(-120, 310, [[images objectAtIndex:0] size].width, [[images objectAtIndex:0] size].height);

然后定义路径:

CGMutablePathRef aPath;
CGFloat arcTop = insect.center.y - 50;
aPath = CGPathCreateMutable();

CGPathMoveToPoint(aPath, NULL, insect.center.x, insect.center.y);
CGPathAddCurveToPoint(aPath, NULL, insect.center.x, arcTop, 240, -100, 490, 360);

CAKeyframeAnimation* arcAnimation = [CAKeyframeAnimation animationWithKeyPath: @"position"];
arcAnimation.repeatCount = HUGE_VALF;
[arcAnimation setDuration: 4.5];
[arcAnimation setAutoreverses: NO];
arcAnimation.removedOnCompletion = NO;
arcAnimation.fillMode = kCAFillModeBoth; 
[arcAnimation setPath: aPath];
CFRelease(aPath);
[insect.layer addAnimation: arcAnimation forKey: @"position"];

我将如何执行无限循环路径留给您:)

希望它有所帮助!

You may use CoreAnimation. You can subclass a view, create a subview for the insect, and then assign an animation to it, following a defined path.

Your UIImageView could be animated. If it's a fly, you can do a few frames for wing moves:

NSArray *images = [NSArray arrayWithObjects:..., nil];

insect.animationImages = images;
insect.animationDuration = ??;
insect.animationRepeatCount = 0;
[insect startAnimating];

Then set an init frame for the insect:

insect.frame = CGRectMake(-120, 310, [[images objectAtIndex:0] size].width, [[images objectAtIndex:0] size].height);

And then define the path:

CGMutablePathRef aPath;
CGFloat arcTop = insect.center.y - 50;
aPath = CGPathCreateMutable();

CGPathMoveToPoint(aPath, NULL, insect.center.x, insect.center.y);
CGPathAddCurveToPoint(aPath, NULL, insect.center.x, arcTop, 240, -100, 490, 360);

CAKeyframeAnimation* arcAnimation = [CAKeyframeAnimation animationWithKeyPath: @"position"];
arcAnimation.repeatCount = HUGE_VALF;
[arcAnimation setDuration: 4.5];
[arcAnimation setAutoreverses: NO];
arcAnimation.removedOnCompletion = NO;
arcAnimation.fillMode = kCAFillModeBoth; 
[arcAnimation setPath: aPath];
CFRelease(aPath);
[insect.layer addAnimation: arcAnimation forKey: @"position"];

I leave how to do the infinite loop path up to you :)

Hope it helps!

清旖 2024-12-16 02:46:18

通常,如果您要移动物体,我建议使用 [UIView animate...]。然而,您希望某些东西在复杂、弯曲的路径上移动。因此,我建议提出一个方程,给出昆虫的 (x,y) 作为时间的函数,然后以相当小的时间间隔启动 NSTimer,每次获得更新时,移动昆虫(也许使用 [UIView animate...])。

另一种方法是使用二维动画框架,例如 cocos2d - 然后,您可以获得与帧刷新率相关联的“更新”调用,在其中使用与以下相同的方程更新昆虫的位置多于。

Normally, if you were to be moving things around, I'd suggest using [UIView animate...]. However, you want something to move on a complex, curvy path. So instead, I'd suggest coming up with an equation that gives the (x,y) for the insect as a function of time, and then start an NSTimer with a fairly small time interval, and every time you get an update, move the insect (perhaps using [UIView animate...]).

Another way to go is to use a 2-d animation framework such as cocos2d - then, you can get an 'update' call linked to the frame refresh rate, inside of which you update the position of your insect using the same equation as from above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文