将 NSTimeInterval 格式设置为分钟:秒:毫秒

发布于 2024-12-05 10:39:53 字数 481 浏览 3 评论 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];
}

Can anyone please tell me how to display milliseconds in this piece of code?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

‖放下 2024-12-12 10:39:53
[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]; 
}

如果有人想要将毫秒设置为最大 60,则将此行编辑

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

int millisec = ((milli) % 60);
[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);
但可醉心 2024-12-12 10:39:53

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

NSInteger milliseconds = (sec * 1000);

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

I don't understand. Don't you just have to create a new variable for the milliseconds?

NSInteger milliseconds = (sec * 1000);

durationLabel.text = [NSString stringWithFormat:@"-%02d:%02d.%04d",min,sec, milliseconds, nil];
愛放△進行李 2024-12-12 10:39:53

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

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 在其参数列表的末尾。

I think this is the simplest solution:

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

That formats the time as MM:SS.sss. If you really want it as MM:SS:sss (which I don't recommend), you can do this:

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

Note that stringWithFormat: does not require nil at the end of its argument list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文