2012-05-02 133 views
1

我有一個在我的代碼中運行的檢查。如果檢查返回true,我執行一個動畫並向用戶顯示一個UIAlertView。我的問題是,我不知道如何延遲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

切換到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]; 
    }]; 
} 
+0

就這麼簡單!謝謝。你能告訴我1.0f中「f」的意義嗎? – garethdn

+0

「f」表示它是一個浮點數。 –

+0

「它」是指秒數?這是必要的還是僅僅是良好的做法?我見過幾個沒有'f'的例子。 – garethdn

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,以上是解決你的問題的方法。