Right now I know how to move an object in a certain direction at a set speed but am not sure how to move an imageview from one CGPoint to another along a set path without using animations so it can be interactive. Also is there a way to apply a slope while it's traveling from point A to B so it appears to curve as it moves. What I'm trying to achieve is a bird swooping effect toward the ground then fly back up in the air.
A:
You can achieve this by using an NSTimer
which periodically calls a method of yours that incrementally moves the bird (your animated image) a little bit each time.
Some sample code to move an object along a parabolic arc:
// 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);
}
If you want a lot of animation, you might also consider using OpenGL, or writing a custom UIView
subclass which overrides the drawRect:
method, which might be a little more efficient (than the above code).
Tyler
2009-08-18 18:58:04