UIView动画块无法正常工作

the*_*ick 0 iphone xcode objective-c ios

一旦游戏中剩下不到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];
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,我在另一个应用程序中使用完全相同的代码块.也许我班上的某个地方有另一个动画干扰了这个动画?

Ned*_*Ned 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;
                     }];
}
Run Code Online (Sandbox Code Playgroud)