2011-10-27 25 views
0

類似SO質疑裁判:Why does UIView:animateWithDuration complete immediately?然而,我的代碼使用[UIView的beginAnimation]等ipad:爲什麼我的順序動畫的第一個動畫總是立即完成?

我基本上這樣的:

[UIView beginAnimation ...]; 
[UIView setAnimationDelay: 0.0]; 
[UIView setAnimationDuration: 1.25]; 
animatedImage.transform = "scale-up transform"; 

[UIView setAnimationDelay: 1.25] 
[UIView setAnimationDuration: 0.50]; 
animatedImage.transform = "scale-down transform"; 

[UIView commitAnimation]; 

的圖像立即跳轉到擴大規模的大小,然後1.25秒後,它很好地適應「縮小」尺寸。如果我鏈接更多的序列,它們都可以正常工作,除了第一個。

回答

1

當您將動畫放入同一個beginAnimation區域時,它們將同時進行動畫處理。通過調用[UIView setAnimationDelay:1.25],你只覆蓋你以前的[UIView setAnimationDelay:0.0]。

所以會發生什麼,是UIView被告知同時向上和向下縮放。我想,既然你告訴它可以向上和向下縮放,它只是跳過到最後一個動畫,但你確實告訴它要放大,所以它沒有動畫。

我建議使用代替塊語法,它可以讓你的動畫完成後做的事情:

[UIView animateWithDuration:1.25 
       animations:^{animatedImage.transform = "scale-up transform";} 
       completion:^(BOOL finished) 
       { 
        [UIView animateWithDuration:1.25 
             animations:^{animatedImage.transform = "scale-down transform";} 
        ]; 
       } 
]; 

在完成塊中的代碼(^ {代碼}結構被稱爲「塊」)是在第一次動畫之後發生的事情。你可以保持鏈接這個儘可能多的動畫,只要你喜歡。

(BOOL finished)是與塊一起傳遞的參數。它告訴動畫是否真的完成。如果否,您的動畫被中斷。

+0

這是否與嵌套的beginAnimations功能相同?因爲我嘗試過並且發生同樣的失敗。同時,我會嘗試您的建議並回復給您(感謝您的快速回復)。 – mobibob

+0

如果您使用兩個獨立的beginAnimations,則第二個將停止第一個。在第二個延遲可能工作,從來沒有測試過,但我認爲使用beginAnimationsWithDuration更好,因爲你可以設置持續時間。完成也比延遲處理更穩定,另外還有另外一個版本的beginAnimations,它提供了更多參數,可以讓您在動畫中設置特殊選項。 – Aberrant

+0

對不起,我不是這個意思。該文件說,你可以嵌套[UIView beginAnimation] [UIView beginAnimation [UIView commitAnimation] [UIView commitAnimation]。這相當於你的代碼嗎? – mobibob

相關問題