garethdn
garethdn

Reputation: 12373

Setting a delay on a UIAlertView

I have a a check that is run in my code. If the check returns true, I execute an animation and display a UIAlertView to the user. My problem is that i don't know how to delay the UIAlertView until the animation is completed. So, currently, the UIAlertView is displayed and the animation is seen running in the background. I'd appreciate any help with this. Here is the relevant code:

BOOL isComplete = [self checkJigsawCompleted:droppedInPlace withTag:tag];
        if (isComplete) {

            [stopWatchTimer invalidate];
            stopWatchTimer = nil;
            [self updateTimer];

            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:1];
            imgGrid.alpha = 0;
            imgBackground.alpha = 0;
            [UIView commitAnimations]; 
            NSString *completedMessage = [NSString stringWithFormat:@"You completed the puzzle in: %@", lblStopwatch.text];


            UIAlertView *jigsawCompleteAlert = [[UIAlertView alloc]   //show alert box with option to play or exit
                                  initWithTitle: @"Congratulations!" 
                                  message:completedMessage 
                                  delegate:self 
                                  cancelButtonTitle:@"I'm done" 
                                  otherButtonTitles:@"Play again",nil];
            [jigsawCompleteAlert show];
        }

Upvotes: 1

Views: 811

Answers (2)

sergio
sergio

Reputation: 69027

You can simply add an handler for when your animation is completed:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
...

Of course, you should provide an implementation for animationDidStop:finished:context: where you display the dialog.

Keep in mind that beginAnimations and its family of methods are discouraged as of iOS 4.0 and block based animations are the preferred way to go. But if you want to support iOS 3.x, the above is the solution to your problem.

Upvotes: 1

Ash Furrow
Ash Furrow

Reputation: 12421

Switch to the blocks-based animation method:

if (isComplete) {

    [stopWatchTimer invalidate];
    stopWatchTimer = nil;
    [self updateTimer];

    [UIView animateWithDuration:1.0f animations:^{
        imgGrid.alpha = 0;
        imgBackground.alpha = 0;
    } completion:^(BOOL finished) {
        NSString *completedMessage = [NSString stringWithFormat:@"You completed the puzzle in: %@", lblStopwatch.text];
        UIAlertView *jigsawCompleteAlert = [[UIAlertView alloc]   //show alert box with option to play or exit
                                            initWithTitle: @"Congratulations!" 
                                            message:completedMessage 
                                            delegate:self 
                                            cancelButtonTitle:@"I'm done" 
                                            otherButtonTitles:@"Play again",nil];
        [jigsawCompleteAlert show];
    }];
}

Upvotes: 2

Related Questions