在标签中显示一个数字,但要计算数字

时间:2013-10-05 23:27:07

标签: iphone ios objective-c uilabel

我的课程中有一个UILabel,我希望视图显示数字,但要计算数字。例如,如果数字是368,那么它会迅速攀升至250-300,然后在达到数字时开始减速。现在这个数字不会总是368,数字会有所不同。它可能是6或129或1023所以它不能是一个固定的数字。我显然知道如何创建UILabel因为我已经在我的应用程序中使用它,我只需要帮助向上计数。

我有一个“玩家视图”,等等等等,他们得到一个分数,然后传递给另一个视图“Gameover视图”,分数显示在UILabel

以下是传递数据的“播放器视图”的代码:

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

以下是传递数据的“Gameover视图”的代码:

- (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];
}

提前致谢!

4 个答案:

答案 0 :(得分:1)

我自己想通了。我找到了这个控件,它就像我需要它一样工作! Click Here

答案 1 :(得分:0)

您可以使用Timer更新循环中的UILabel对象。随着时间的推移,在一定的时间间隔内,通过增加时间差来“减慢速度”。通过Timer更新UI对象的示例代码是Internet上常见的代码,因为它是您编写进度条的方式。

how to use the progress bar in the iphone app

答案 2 :(得分:0)

使用NSTimer

每隔一定时间间隔(每0.1秒一次)调用此方法

它会将值增加到目标数字。

-(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];
    }


}

对于计时器:

[NSTimer scheduledTimerWithInterval:0.1 
                             target:self 
                           selector:@selector(countUp:) 
                           userInfo:nil 
                            repeats:YES];

答案 3 :(得分:0)

使用文字可以很容易地播放

 UILabel * label;
 label.text = @(label.text.integerValue+1).stringValue;