I've got an UILabel that displays users score. And score changes from time to time, is there a way to animate this change, to slowly increment this number from its current value to its result value? Something like http://josheinstein.com/blog/index.php/2010/02/silverlight-animated-turbotax-number-display/ but for objective-c.
Asked
Active
Viewed 3,630 times
8
-
I found that might be helpful http://stackoverflow.com/questions/3073520/setting-new-text-to-uilabel-and-animation **First look for answer** – wczekalski Oct 17 '11 at 20:08
-
1Yeah, I saw it, not exactly animation I'm looking for. – Drabuna Oct 17 '11 at 20:09
2 Answers
18
Use a CADisplayLink to change the text property of custom subclass of UILabel over some period of time. You'll probably want to use a NSNumberFormatter
for prettier output.
// Create instance variables/properties for: `from`, `to`, and `startTime` (also include the QuartzCore framework in your project)
- (void)animateFrom:(NSNumber *)aFrom toNumber:(NSNumber *)aTo {
self.from = aFrom; // or from = [aFrom retain] if your not using @properties
self.to = aTo; // ditto
self.text = [from stringValue];
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(animateNumber:)];
startTime = CACurrentMediaTime();
[link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)animateNumber:(CADisplayLink *)link {
static float DURATION = 1.0;
float dt = ([link timestamp] - startTime) / DURATION;
if (dt >= 1.0) {
self.text = [to stringValue];
[link removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
return;
}
float current = ([to floatValue] - [from floatValue]) * dt + [from floatValue];
self.text = [NSString stringWithFormat:@"%i", (long)current];
}

Jason Harwig
- 43,743
- 5
- 43
- 44
-
3Note, if you do use an `NSNumberFormatter`, create one of them and re-use it for every formatting job that uses the same format. Creating a new `NSNumberFormatter` is quite expensive, but re-using an existing one is cheap. – Lily Ballard Oct 17 '11 at 20:50
-
I keep getting EXC_BAD_ACESS at `float current = ([to floatValue] - [from floatValue]) * dt + [from floatValue]`; – Drabuna Oct 18 '11 at 10:37
-
are you using properties for the `from` and `to` variables? and is it set to `retain`? If you're not using properties do `from = [aFrom retain]` – Jason Harwig Oct 18 '11 at 18:02
-
It works great on simulator, but on iphone, it doesn't. It displays 1 number between "from" and "to", and then "to" number. Seems like it lags. Any advice? – Drabuna Oct 21 '11 at 14:59
-
4
AUIAnimatedText
has all you was asking for. That is replacement for UILabel with reach text animating possibilities ,

wczekalski
- 720
- 4
- 21