简体   繁体   English

IOS:移动UIImageView

[英]IOS: move an UIImageView

In my app I want to move a little UIImageView with inside a .png; 在我的应用程序中,我想在.png内部移动一个小UIImageView; 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 ∞ 例如,我希望当png将无效的simbol∞移动为倒数8时,这个png会做

You may use CoreAnimation. 您可以使用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. 你的UIImageView可以动画。 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! 希望能帮助到你!

Normally, if you were to be moving things around, I'd suggest using [UIView animate...]. 通常,如果你要移动东西,我建议使用[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...]). 所以相反,我建议给出一个方程式,给出昆虫的(x,y)作为时间的函数,然后以相当小的时间间隔启动NSTimer,每次获得更新时,移动昆虫(也许使用[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. 另一种方法是使用二维动画框架,如cocos2d - 然后,您可以获得一个链接到帧刷新率的“更新”调用,在其中使用相同的等式更新昆虫的位置以上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM