2011-09-20 117 views
0

任何人都可以請告訴我如何在這段代碼中顯示毫秒數?格式NSTimeInterval分鐘:秒:毫秒

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

回答

0

我不明白。你不需要在毫秒內創建一個新的變量嗎?

NSInteger milliseconds = (sec * 1000); 

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

這就是我所做的,它仍然以秒爲單位..例如 - 02:05.05000,02:06.0600 ...我錯過了我的計算中乘以秒的東西.... –

+0

哦對...。我的大腦剛剛開始工作...... 05000毫秒是5秒..!Pft!...你有什麼想法我怎麼能讓這個計時器顯示每毫秒而不是1000組?例如 - 02:05.1234,02:05.1235 ...? –

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); 
+0

如果有任何想要的毫秒到最大60比edi這行int millisec =((milli)%60)+39;詮釋millisec =((毫)%60); – SALMAN

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