0

谁能告诉我如何在这段代码中显示毫秒?

-(void)updateViewForPlayerInfo:(AVAudioPlayer*)p {
    countdownTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTimeLeft)userInfo:p repeats:YES];
}

- (void)updateTimeLeft {
    NSTimeInterval timeLeft = player.duration - player.currentTime;

    int min=timeLeft / 60;
    int sec=lroundf(timeLeft) % 60;

    durationLabel.text = [NSString stringWithFormat:@"-%02d:%02d",min,sec,nil];
}
4

3 回答 3

1
[NSTimer scheduledTimerWithTimeInterval:0.0167 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];

-(void)updateTimer {
    countDownTotalTimer++;     
    TotalTime.text=[self timeFormatted:countDownTotalTimer];
}

- (NSString *)timeFormatted:(int)milli
{
    int millisec = ((milli) % 60)+39; 
    int sec = (milli / 60) % 60; 
    int min = milli / 3600; 

    return [NSString stringWithFormat:@"%02d:%02d:%02d",min, sec, millisec]; 
}

If any wants milliseconds to max 60 than edit this line

int millisec = ((milli) % 60)+39;  

to

int millisec = ((milli) % 60);
于 2012-06-22T13:28:30.333 回答
0

我不明白。您不必为毫秒创建一个新变量吗?

NSInteger milliseconds = (sec * 1000);

durationLabel.text = [NSString stringWithFormat:@"-%02d:%02d.%04d",min,sec, milliseconds, nil];
于 2011-09-20T03:48:13.260 回答
0

我认为这是最简单的解决方案:

durationLabel.text = [NSString stringWithFormat:@"-%02d:%06.3f",
    (int)(timeLeft / 60), fmod(timeLeft, 60)];

将时间格式化为MM:SS.sss. 如果你真的想要它MM:SS:sss(我不推荐),你可以这样做:

durationLabel.text = [NSString stringWithFormat:@"-%02d:%02d:%03.0f",
    (int)(timeLeft / 60), (int)fmod(timeLeft, 60), 1000*fmod(timeLeft, 1)];

请注意,stringWithFormat:不需要nil在其参数列表的末尾。

于 2015-08-19T19:59:07.600 回答