UIView Animation block not working
I am trying to make my timer blink red once there is less than 10 seconds left in the game. For some reason, the animation is not working. The timeLabel just turns white and stays that way. Here is the code I'm using:
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];
}
What's weird is that I开发者_开发知识库 use this exact same code block in another app which works perfectly. Perhaps I have another animation somewhere in my class that is interfering with this one?
Text color isn't an animatable property. You can try using CATextLayer to do this instead.
Another option is to have an identical UILabel with red text sitting on top of the white UILabel and fade that from transparent to opaque and back. That would look like this:
if (timeLeft <= 9 && timeLeft > 0) {
redTimeLabel.alpha = 0;
[UIView animateWithDuration:1
animations:^{
redTimeLabel.alpha = 1;
}
completion:^(BOOL finished){
redTimeLabel.alpha = 0;
}];
}
Looks to me like you've forgot
[UIView setAnimationDidStopSelector:
@selector(animationFinished:finished:context:)];
Also, if the block you've posted runs in the main loop, are you checking whether the animation is already taking place? Like some redBlinkingDidStart boolean or something... Just in case :P
精彩评论