iOS 在 UILabel 上将时间显示为分数和高分

标签 ios nstimer scoring

我在尝试将分数显示为高分时遇到问题。

这是我的代码,用于以正确的格式在标签上显示时间。

- (void)populateLabelwithTime:(int)milliseconds {

    int seconds = milliseconds / 1000;
    int minutes = seconds / 60;
    int hours = minutes / 60;

    seconds -= minutes * 60;
    minutes -= hours * 60;


    resultScoreLabel.text = [NSString stringWithFormat:@"%ds:%dms",seconds, milliseconds%1000];

    if (currentTime > highScore) {
        [[NSUserDefaults standardUserDefaults] setInteger:currentTime forKey:@"highscore"];
        resultHighScoreLabel.text = [NSString stringWithFormat:@"%i",highScore +10];
    }
    else {
    }
}

-(void)scoring {

   currentTime += 10;
   [self populateLabelwithTime:currentTime];
   highScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"highscore"];
}

乐谱格式正确。 得分为:1s:186ms

但是高分出现错误 您的最高分是:1186

知道这一行是错的

resultHighScoreLabel.text = [NSString stringWithFormat:@"%i",highScore +10];

但我不知道如何让它工作

有什么建议吗??

最佳答案

这是快速修复:

int newHighscore = highScore+10;

int seconds = ((int)floor((newHighscore)/1000));
int milliseconds = newHighscore%1000;

resultHighScoreLabel.text = [NSString stringWithFormat:@"%is:%ims",seconds,milliseconds];

但是我强烈建议改变你处理高分的方式

关于iOS 在 UILabel 上将时间显示为分数和高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23971397/

相关文章:

objective-c - Objective-C 中的全局字符串变量

ios - NSTimer递增一次后停止

ios - 切换 View Controller 后 NSTimer 触发两次

solr 评分 - fieldnorm

php - Symfony2 - 如何检查我们是否被移动设备调用

ios - 在 InputBarAccessoryView TextField 中处理(或禁用)Memoji

swift - NSTimer 倒计时产生两次 0

python - scikit-learn 中聚类超参数评估的网格搜索

lucene - Lucene 6.6.0 中索引时间字段级别提升?

android - react-native cli 和 Expo with Bare 工作流有什么区别?