1

我有一个在我的代码中运行的检查。如果检查返回 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];
        }
4

2 回答 2

2

切换到基于块的动画方法

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];
    }];
}
于 2012-05-02T15:48:13.897 回答
1

您可以简单地为动画完成添加一个处理程序:

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

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

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

于 2012-05-02T15:50:55.097 回答