0

I have a UILabel in my class and I would like the view to display the number but to count up to the number. For example, If the number was 368, it would climb quickly to the 250-300's then start to slow down as it gets to the number. Now this number will not always be 368, the number varies. It could be 6 or 129 or 1023 so it can't be a set-in-stone number. I obviously know how to create the UILabel as I already have it being used in my app, I just need help with counting upwards to it.

I have a "Player view", blah blah blah they get a score and it is passed to another view "Gameover view" and the score is display in a the UILabel.

Here is the code for the "Player view" to pass the data:

//finalScore is a int and so is currentScore
    second.finalScore = self.currentScore;

Here is the code for the "Gameover view" to pass the data:

- (void)viewWillAppear:(BOOL)animated {
    // scoreLabel is the IBOutlet to the UILabel in IB for displaying the score.    
    self.currentIndex++;
    if(self.currentIndex == [self.numbersArray count]) self.currentIndex = 0;

    self->scoreLabel.text = [NSString stringWithFormat:@"%d",self->finalScore];
}

Thanks in advance!

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Kyle Greenlaw
  • 737
  • 1
  • 8
  • 20
  • 1
    possible duplicate of [Animating a UITextView increasing or decreasing the value of a number](http://stackoverflow.com/questions/18807849/animating-a-uitextview-increasing-or-decreasing-the-value-of-a-number) – pasawaya Oct 06 '13 at 00:27

4 Answers4

1

I figured it out myself. I found this control and it works just as I need it to! Click Here

Kyle Greenlaw
  • 737
  • 1
  • 8
  • 20
0

You can update the UILabel object in a loop, using the Timer. As time progresses at a certain interval, "slow it down" by adding on the difference in time. Sample code for updating a UI object via Timer is a common bit of code on the Internet, as it's the way you code progress bars.

how to use the progress bar in the iphone app

Community
  • 1
  • 1
Anna Billstrom
  • 2,482
  • 25
  • 33
0

Just call this method every certain interval (ex every 0.1 seconds), using a NSTimer

It will increment the value up to the target number.

-(void)countUp:(id)sender {
    int targetNumber = yourNumber;
    int currentNumber = [[myLabel text] intValue];

    // Calculate the increment for this time. Mess with this for different speeds. 
    // Just multiply some of the numbers by some constants (ex. 2) until you 
    // achieve the desired effect.
    double increment = (targetNumber - currentNumber) / targetNumber;
    NSString *newValue = [NSString stringWithFormat:@"%f",currentNumber + increment];
    [myLabel setText:newValue];
    if ([newValue doubleValue] >= targetValue) {
        [(NSTimer *)sender invalidate];
    }


}

For the timer:

[NSTimer scheduledTimerWithInterval:0.1 
                             target:self 
                           selector:@selector(countUp:) 
                           userInfo:nil 
                            repeats:YES];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Andrew
  • 15,357
  • 6
  • 66
  • 101
  • @KyleGreenlaw change yourNumber to whatever represents the number that you want your label to eventually show. – Andrew Oct 05 '13 at 23:48
  • I think I got it, I will do a test and let you know but I think this is the answer I need. – Kyle Greenlaw Oct 05 '13 at 23:48
  • When the view shows, the number is just (in this case) 302.00000000 and did not count up – Kyle Greenlaw Oct 06 '13 at 00:00
  • @KyleGreenlaw did you do `[myLabel setText:]` ? If you do, then delete that line because you don't want to do that because then it won't increment. – Andrew Oct 06 '13 at 00:04
  • @KyleGreenlaw Also, I just changed something in my answer. Update your code for that. Also, I just realized that you may need to rewrite the increment calculation completely if it goes up to the wrong number. Im not sure how accurate it will be :/ – Andrew Oct 06 '13 at 00:08
  • @KyleGreenlaw On this line: `self->scoreLabel.text = [NSString stringWithFormat:@"%d",self->finalScore];` you need to start the timer as described in answer – Andrew Oct 06 '13 at 00:31
  • @KyleGreenlaw In the method that is described in the answer, you need to change all references of targetNumber to self->finalScore and remove targetNumber's declaration. – Andrew Oct 06 '13 at 00:34
  • Ok, and how would start the timer on that line? – Kyle Greenlaw Oct 06 '13 at 00:37
  • @KyleGreenlaw See bottom of my answer. – Andrew Oct 06 '13 at 00:37
  • Im am get the nsstring to nstimer alert. – Kyle Greenlaw Oct 06 '13 at 00:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38676/discussion-between-kyle-greenlaw-and-santa-claus) – Kyle Greenlaw Oct 06 '13 at 00:40
0

using literals it become quite easy to play

 UILabel * label;
 label.text = @(label.text.integerValue+1).stringValue;
Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81