Iphone UIView动画块不工作

Iphone UIView动画块不工作,iphone,objective-c,xcode,ios,Iphone,Objective C,Xcode,Ios,我试图使我的计时器闪烁红色一旦有不到10秒,在游戏中剩下的。由于某些原因,动画无法工作。时间标签只是变白并保持不变。以下是我正在使用的代码: if (timeLeft <= 9 && timeLeft > 0) { timeLabel.textColor = [UIColor redColor]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1]

我试图使我的计时器闪烁红色一旦有不到10秒,在游戏中剩下的。由于某些原因,动画无法工作。时间标签只是变白并保持不变。以下是我正在使用的代码:

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];
}
if(timeLeft 0){
timeLabel.textColor=[UIColor redColor];
[UIView beginAnimations:nil上下文:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
timeLabel.textColor=[UIColor-whiteColor];
[UIView委员会];
}

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

在我看来,你好像忘了

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

此外,如果已发布的块在主循环中运行,是否正在检查动画是否已经发生?比如一些红色闪烁的DIDSTART布尔值之类的。。。以防万一:在我看来,你好像忘记了

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

此外,如果已发布的块在主循环中运行,是否正在检查动画是否已经发生?比如一些红色闪烁的DIDSTART布尔值之类的。。。以防万一:P

文本颜色不是可设置动画的属性。您可以尝试使用来执行此操作

另一个选择是在白色UILabel的顶部放置一个相同的UILabel,红色文本置于白色UILabel之上,然后将其从透明变为不透明再变回透明。看起来是这样的:

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

文本颜色不是可设置动画的属性。您可以尝试使用来执行此操作

另一个选择是在白色UILabel的顶部放置一个相同的UILabel,红色文本置于白色UILabel之上,然后将其从透明变为不透明再变回透明。看起来是这样的:

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

这是一个很好的解决方案。谢谢这是一个很好的解决方案。谢谢