1

I'm trying to make a countdown timer which also changes it's color.

I'm trying to animate the UILabel during the whole countdown, like one long animation. So when the timer is at 60 seconds, the textColor is black, at 30 it would seem gray and at 0, the textColor is white.

The problem I'm having is that the color changes perfectly over time, but that the text of the label isn't changed. It looks like the label is frozen until the color animation stops. After that, the label is counting down normally.

This is the code I'm using:

- (IBAction) startTimerWithButton:(UIButton *) sender {
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCounterWithTimer:) userInfo:nil repeats:YES];

        [UIView transitionWithView:self.timeLabel duration:(self.timeInterval / 2)
                           options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCrossDissolve)
                        animations:^{ self.timeLabel.textColor = [UIColor whiteColor]; }
                        completion:nil];
    }
    else {
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (void) updateCounterWithTimer:(NSTimer *) theTimer {
    if (self.timeInterval > 0) {
        self.timeInterval--;
        self.timeLabel.text = [NSString stringWithFormat:@"%02d", (int) self.timeInterval];
    }
}

Am I missing an UIViewAnimationOption here? Is there a way to combine those animations (changing text of the UILabel and changing of the textColor)?

Jeroen de Leeuw
  • 907
  • 1
  • 9
  • 18
  • Have a look at the [list of UIView animatable properties](https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/animatingviews/animatingviews.html) – Aaron Brager Jun 05 '14 at 15:21
  • Also, this is a possible duplicate of [Animate UILabel text between two numbers?](http://stackoverflow.com/questions/5301305/animate-uilabel-text-between-two-numbers) – Aaron Brager Jun 05 '14 at 15:22
  • @AaronBrager It's not, I'm trying to animate the UILabel during the whole countdown, like one long animation. So when the timer is at 60 seconds, the textColor is black, at 30 it would seem gray and at 0, the textColor is white. – Jeroen de Leeuw Jun 05 '14 at 15:29
  • @AaronBrager I know that textColor isn't an animatable property by default, but when you use the UIView transitionWithView, you can change the textColor of a Label. The question is: why can't I animate it and change the text of the Label? – Jeroen de Leeuw Jun 05 '14 at 15:32

0 Answers0