移动一个imageview alonge一条路

时间:2009-08-18 18:43:42

标签: iphone

现在我知道如何以设定的速度在某个方向上移动一个物体,但我不知道如何在不使用动画的情况下沿着设定路径将一个CGPoint从一个CGPoint移动到另一个CGPoint,因此它可以是交互式的。还有一种方法可以在从A点到B点移动时应用斜率,因此它在移动时似乎会弯曲。我想要实现的是对地面的鸟类俯冲效果,然后在空中飞回来。

1 个答案:

答案 0 :(得分:0)

你可以使用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,或者编写一个自定义的UIView子类来覆盖drawRect:方法,这可能会比上面的代码更有效率)。

相关问题