2011-02-18 141 views
0

我試圖讓我的計時器在遊戲中還剩下不到10秒時閃爍紅色。出於某種原因,動畫不起作用。 timeLabel只是變成白色並保持這種狀態。這裏是我正在使用的代碼:UIView動畫塊無法正常工作

if (timeLeft <= 9 && timeLeft > 0) { 
    timeLabel.textColor = [UIColor redColor]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationDelegate:self]; 
    timeLabel.textColor = [UIColor whiteColor]; 
    [UIView commitAnimations]; 
} 

奇怪的是,我在另一個完全相同的應用程序中使用完全相同的代碼塊。也許我在我的班級某處有另一個動畫,正在干擾這個動畫?

回答

5

文字顏色不是一個動畫的屬性。您可以嘗試使用CATextLayer來代替。

另一種選擇是擁有一個相同的UILabel,紅色文本位於白色UILabel之上,並從透明到不透明和背面淡入淡出。這看起來像這樣:

if (timeLeft <= 9 && timeLeft > 0) { 
    redTimeLabel.alpha = 0; 
    [UIView animateWithDuration:1 
        animations:^{ 
         redTimeLabel.alpha = 1; 
        } 
        completion:^(BOOL finished){ 
         redTimeLabel.alpha = 0; 
        }]; 
} 
+0

這是一個很好的解決方案。謝謝! – thenameisnick 2011-02-21 18:56:39

0

看起來像你對我已經忘記

[UIView setAnimationDidStopSelector: 
       @selector(animationFinished:finished:context:)]; 

另外,如果您已經發布運行在主循環塊,你檢查動畫是否已經發生?像一些redBlinkingDidStart布爾或什麼的......以防萬一:P