簡體   English   中英

UIView動畫塊不起作用

[英]UIView Animation block not working

一旦游戲中的剩余時間不足10秒,我將嘗試使計時器閃爍紅色。 由於某些原因,動畫無法正常工作。 timeLabel變成白色並保持這種狀態。 這是我正在使用的代碼:

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

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

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

另一個選擇是在白色的UILabel上放置一個帶有紅色文本的相同UILabel,然后將其從透明變淡為不透明,然后再變回。 看起來像這樣:

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

在我看來就像你已經忘記了

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

另外,如果您發布的塊在主循環中運行,您是否正在檢查動畫是否已經在進行? 像一些redBlinkingDidStart布爾值之類的...以防萬一:P

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM