繁体   English   中英

IOS:移动UIImageView

[英]IOS: move an UIImageView

在我的应用程序中,我想在.png内部移动一个小UIImageView; 这是一只小昆虫,我想模拟他的飞行。 例如,我希望当png将无效的simbol∞移动为倒数8时,这个png会做

您可以使用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"];

我留下如何做无限循环路径到你:)

希望能帮助到你!

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

另一种方法是使用二维动画框架,如cocos2d - 然后,您可以获得一个链接到帧刷新率的“更新”调用,在其中使用相同的等式更新昆虫的位置以上。

暂无
暂无

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

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