簡體   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