Paul Peelen
Paul Peelen

Reputation: 10329

Update UILabel text animated

I would like to animate the counting of digets in a text calculating upwards. The texts is in a UILabel similar like You have driven for 0.0km and that needs to be changed to You have driven for 143.6km with count animation. Is there any way I can update it animated?

Edit Here is some of my current code, concerning other animations I already have:

        if (animated)
        {
            [UIView beginAnimations:@"scaleAnimation" context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDuration:animationDuration];
        }

        [...]

        // Amount pointer
        float xForRedBar = redBarFrame.size.width + redBarFrame.origin.x;

        CGRect pointerFrame = cell.amountBarPointer.frame;
        pointerFrame.origin.x = (xForRedBar - (pointerFrame.size.width/2));

        if (pointerFrame.origin.x < 12)
            pointerFrame.origin.x = 12;

        if (pointerFrame.origin.x >= (308 - (pointerFrame.size.width/2)))
            pointerFrame.origin.x = 308 - pointerFrame.size.width;

        [cell.amountBarPointer setFrame:pointerFrame];

        // Amount bar
        CGRect amountBarFrame = cell.amountBar.frame;
        amountBarFrame.origin.x = 9+(((302 - amountBarFrame.size.width)/100)*self.procentCompleted);

        [cell.amountBar setFrame:amountBarFrame];

        // Amount info text
        CGRect amountInfoFrame = cell.amountInfo.frame;
        amountInfoFrame.origin.x = amountBarFrame.origin.x + 2;

        [cell.amountInfo setFrame:amountInfoFrame];

        // Amount text
        [cell.amountInfo setText:[NSString stringWithFormat:NSLocalizedString(@"You have driven for %@km", nil), self.userAmount]];

        [...]

        if (self.procentCompleted == 0)
        {
            [cell.amountBar setAlpha:0];
            [cell.amountBarPointer setAlpha:0];
            [cell.amountInfo setAlpha:0];
        }
        else {
            [cell.amountBar setAlpha:1];
            [cell.amountBarPointer setAlpha:1];
            [cell.amountInfo setAlpha:1];
        }

        if (animated)
        {
            [UIView commitAnimations];
        }

Upvotes: 0

Views: 1740

Answers (1)

rdelmar
rdelmar

Reputation: 104082

Sure, you can update it "animated", but you have to use a repeating timer, and add one (or whatever interval you want) to the count each time the timer's selector is called. Include a test for the final value, and invalidate the timer when you get there.

After Edit:

If you want the speed of the count to slow down toward the end, I wouldn't use a timer, I would use performSelector:withObject:afterDelay:. To make it repeat, you would call this method from within its selector. You would need to do some test to see if you're near the end of the count up, and then add a little bit of time to the delay with each pass. Something like this:

-(IBAction)countUp:(id)sender {
    [self performSelector:@selector(countUpLabel) withObject:nil afterDelay:.1];
}

-(void)countUpLabel {
    static float delay = .01;
    num+= 1;
    label.text = [NSString stringWithFormat:@"%d",num];
    if (num < 40) {
        [self performSelector:@selector(countUpLabel) withObject:nil afterDelay:.1];
    }else if (num > 35 && num <50) {
        [self performSelector:@selector(countUpLabel) withObject:nil afterDelay:.1 + delay];
        delay += 0.01;
    }
}

Upvotes: 1

Related Questions