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

この行を編集するよりもミリ秒を最大60にしたい場合

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

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 に答える