sridvijay
sridvijay

Reputation: 1526

Animating dynamic integer value on iOS?

I want to animate my UILabel whenever the text changes. For some context: I have a label which has the current temperature in it (say 57º) I want to animate it so it goes from 0º to 57º counting upwards. Here's a video which shows what I want: http://www.youtube.com/watch?v=mXfOvGflVWw (It's found in the National Geographic Parks app for iPhone and iPad, in the stats menu) If anyone could point me in the right direction on how to do so, it would be wonderful. I would have used something like NSTimer, but these values are dynamic...

Thanks!

Upvotes: 2

Views: 1293

Answers (1)

omz
omz

Reputation: 53561

Certainly not the most efficient, but a very easy way to do this without the need for additional instance variables:

NSInteger fromValue = 0;
NSInteger toValue = 57; //In this example toValue has to be greater than fromValue
NSString *suffix = @"°";
NSTimeInterval interval = 0.016; //Adjust for different animation speed
NSTimeInterval delay = 0.0;
for (NSInteger i = fromValue; i <= toValue; i++) {
    NSString *labelText = [NSString stringWithFormat:@"%i%@", i, suffix];
    [myLabel performSelector:@selector(setText:) withObject:labelText afterDelay:delay];
    delay += interval;
}

Upvotes: 3

Related Questions