[英]Moving an imageview alonge a path
現在,我知道如何以設定的速度在某個方向上移動對象,但是不確定如何在不使用動畫的情況下將圖像視圖從一個CGPoint沿設定的路徑移動到另一個CGPoint,而不使用動畫,因此它可以是交互式的。 還有一種方法可以在從點A到點B行駛時應用斜率,以使其在移動時似乎彎曲。 我想要實現的是將鳥撲向地面的效果,然后飛回空中。
您可以通過使用NSTimer
來實現此目的,該NSTimer
定期調用您的方法,每次該方法都會逐漸移動小鳥(您的動畫圖像)。
一些示例代碼可沿拋物線弧移動對象:
// Assumptions: We have the variable
// UIView* bird <-- to be moved
// parabolically along the path y = x^2,
// up to translation (that is, slid over so the bottom
// point is not (0,0), but something in the view).
// Suppose bird starts out at (-3, 9) here.
- (void) oneBirdStep {
static const flost ddy = 2;
static float dy = -5; // Start out at 2*x0 + 1.
CGFloat birdY = bird.frame.origin.y + dy;
dy += ddy;
bird.frame = CGRectMake(bird.frame.origin.x + 1, birdY,
bird.frame.size.width, bird.frame.size.height);
}
如果您需要大量動畫,則還可以考慮使用OpenGL,或編寫一個重寫drawRect:
方法的自定義UIView
子類,該子類可能會比上面的代碼效率更高。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.