views:

36

answers:

3

I'm trying to have a button move to a co-ordinates, pause, move to another co-orinates, pause, then move again. the process should then repeat infinitely. what I have right now just moves the last step.

this is what I have so far ('mover' is the UIButton name):

- (void) firstAnimation {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelay:5];
        [UIView setAnimationRepeatCount:-1];
        [UIView setAnimationRepeatAutoreverses:NO];

        CGPoint pos = mover.center;
        pos.y = 200.f;
        pos.x = 169.f;
        mover.center = pos;

        [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(firstAnimation:) userInfo:nil repeats:NO];
        pos.y = 100.f;
        pos.x = 179.f;
        mover.center = pos;

        [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(firstAnimation:) userInfo:nil repeats:NO];
        pos.y = 160.f;
        pos.x = 129.f;
        mover.center = pos;

        [UIView commitAnimations];

}

thanks for any help

+3  A: 

You don't need to use timers, just set the animation delegate to self and implement the animationfinished method.

See my answer here:

http://stackoverflow.com/questions/3909399/3909490#3909490

Ben
A: 

You would be better suited to use CoreAnimation keyframes and setting repeatCount = HUGE_VALF to loop forever

Jason Harwig
+1  A: 

If you're shipping for iOS4 exclusively, I thoroughly recommend using blocks and the following method:

[UIView animateWithDuration:kAnimationDuration
                      delay:kAnimationDelay
                    options:UIViewAnimationCurveEaseInOut
                 animations:^ {
                     // your animations here.
                 }
                 completion:^(BOOL finished) {
                     // what you want to do upon animation completion here.
                 }];

Note that in the completion block you can just as well cue up another animation. In fact, you could store the three animation blocks as block variables and then simply pass them in to the above method to be executed, one after another, until the third completes. Then just restart the process!

Blocks are your friends.

David Foster
thanks for your post. I'm using the following to fade in and out an image. how would I make it repeat infinitely?- (void) firstAnimation { [UIView animateWithDuration:2.0 animations:^{ tapImage.alpha = 0.0; tapImage.alpha = 1.0; } completion:^(BOOL finished){ [self secondAnimation]; }];}
hanumanDev