views:

21

answers:

2

I'm trying to repeat an animation until a certain condition is met. Something like this:

- (void) animateUpdate {
    if (inUpdate) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:2.0];
        [UIView setAnimationDelegate: self];
        [UIView setAnimationDidStopSelector: @selector(animateUpdate)];
        button.transform = CGAffineTransformMakeRotation( M_PI );
        [UIView commitAnimations];
    }
}

This will run the first time, but it won't repeat. The selector will just be called until the application crashes.

How should I do this?

Thanks.

A: 

Core Animation works asynchronously, that is, your code does not block the thread and the animation actually starts at the next pass of the run loop. In order to repeat an animation you should restart it in your animation-did-stop selector or explicitly use CAAnimation's repeatCount, depending on your needs. Otherwise Core Animation will coalesce all your animations into one.

Costique
But that's what the code I wrote should do, right? The AnimationDidStopSelector is the same as the method that begins and commits the animation.What I'm getting is that the animation won't run again and the animateUpdate method will just get called forever.
Tiago
A: 

Same issue. Here is some code that does the same thing

- (void)beginRotation{
  [UIView beginAnimations:@"ghostRotate" context:NULL];
  [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:5.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    ghostImage.transform = CGAffineTransformMakeRotation(M_PI*0.5);
    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
    if ([finished boolValue]){
    [self beginRotation];
    }
}

When the first animation completes, it calls the didStop... This calls the beginAnimation method again which returns immediately to the didStop again. finished is set to 1 and it enters a stack overflow condition...

thoughts?

levous