在UIAlertView上设置延迟

时间:2012-05-02 15:46:15

标签: objective-c ios animation uialertview

我有一个在我的代码中运行的检查。如果检查返回true,我执行动画并向用户显示UIAlertView。我的问题是我不知道如何延迟UIAlertView直到动画完成。因此,目前显示UIAlertView并且可以看到动画在后台运行。我很感激任何帮助。以下是相关代码:

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];
        }

2 个答案:

答案 0 :(得分:2)

切换到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];
    }];
}

答案 1 :(得分:1)

您可以在动画完成时添加处理程序:

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

当然,您应该为显示对话框的animationDidStop:finished:context:提供实现。

请注意,从iOS 4.0开始不鼓励使用beginAnimations及其系列方法,并且基于块的动画是首选方式。但是如果你想支持iOS 3.x,上面就是你问题的解决方案。