How to animate incrementing number in UILabel How to animate incrementing number in UILabel xcode xcode

How to animate incrementing number in UILabel


I actually made such a class just for this called UICountingLabel:

http://github.com/dataxpress/UICountingLabel

It allows you to specify whether you want the counting mode to be linear, ease in, ease out, or ease in/out. Ease in/out starts counting slowly, speeds up, and then finishes slowly - all in whatever amount of time you specify.

It doesn't currently support setting the actual font size of the label based on the current value, though I may add support for that if it's a feature that's in-demand. Most of the labels in my layouts don't have a lot of room to grow or shrink, so I'm not sure how you want to use it. However, it behaves totally like a normal label, so you can change the font size on your own as well.


You can use GCD to shift delays to background threads.

Here is the example of value animation (from 1 to 100 in 10 seconds)

float animationPeriod = 10;dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{    for (int i = 1; i < 101; i ++) {        usleep(animationPeriod/100 * 1000000); // sleep in microseconds        dispatch_async(dispatch_get_main_queue(), ^{            yourLabel.text = [NSString stringWithFormat:@"%d", i];        });    }});


Here @malex's answer in swift 3.

func incrementLabel(to endValue: Int) {    let duration: Double = 2.0 //seconds    DispatchQueue.global().async {        for i in 0 ..< (endValue + 1) {            let sleepTime = UInt32(duration/Double(endValue) * 1000000.0)            usleep(sleepTime)            DispatchQueue.main.async {                self.myLabel.text = "\(i)"            }        }    }}

However, I strongly recommend simply downloading this class from GitHub and dragging it into your project, I've resorted to using it because the timing in my code doesn't seem to adjust properly for lower/higher count numbers. This class works great and looks very good. See this medium article for reference.