1

我非常接近完成我的第一个 iPhone 应用程序,这很开心。我正在尝试通过在 UILabel 上显示当前时间 (NSDate) 的 NSTimer 使用当前时间添加运行时间码。NSDate 对我来说工作正常,显示小时、分钟、秒、毫秒。但是我需要每秒显示 24 帧,而不是毫秒。

问题是我需要每秒帧数与小时、分钟和秒 100% 同步,所以我不能在单独的计时器中添加帧。我试过了,让它工作了,但帧计时器没有与日期计时器同步运行。

谁能帮我解决这个问题?有没有办法自定义 NSDateFormatter 以便我可以将日期计时器格式化为每秒 24 帧?现在我仅限于格式化小时、分钟、秒和毫秒。

这是我现在正在使用的代码

-(void)runTimer {
 // This starts the timer which fires the displayCount method every 0.01 seconds
 runTimer = [NSTimer scheduledTimerWithTimeInterval: .01
            target: self
             selector: @selector(displayCount)
             userInfo: nil
              repeats: YES];
}

//This formats the timer using the current date and sets text on UILabels
- (void)displayCount; {

 NSDateFormatter *formatter =
 [[[NSDateFormatter alloc] init] autorelease];
    NSDate *date = [NSDate date];

 // This will produce a time that looks like "12:15:07:75" using 4 separate labels
 // I could also have this on just one label but for now they are separated

 // This sets the Hour Label and formats it in hours
 [formatter setDateFormat:@"HH"];
 [timecodeHourLabel setText:[formatter stringFromDate:date]];

 // This sets the Minute Label and formats it in minutes
 [formatter setDateFormat:@"mm"];
 [timecodeMinuteLabel setText:[formatter stringFromDate:date]];

 // This sets the Second Label and formats it in seconds
 [formatter setDateFormat:@"ss"];
 [timecodeSecondLabel setText:[formatter stringFromDate:date]];

 //This sets the Frame Label and formats it in milliseconds
 //I need this to be 24 frames per second
 [formatter setDateFormat:@"SS"];
 [timecodeFrameLabel setText:[formatter stringFromDate:date]];

}
4

3 回答 3

1

我建议你从你的中提取毫秒NSDate- 这是以秒为单位的,所以分数会给你毫秒。

NSString然后只需使用普通格式字符串使用方法:附加值stringWithFormat

于 2010-05-17T09:37:53.463 回答
1

NSFormatter + NSDate 的开销很大。另外,在我看来,NSDate 并没有为简单的东西提供“简单”的微时间情况。

Mogga 提供了一个很好的指针,这是一个 C / Objective-C 变体:

- (NSString *) formatTimeStamp:(float)seconds {
    int sec = floor(fmodf(seconds, 60.0f));
    return [NSString stringWithFormat:@"%02d:%02d.%02d.%03d",
                        (int)floor(seconds/60/60),          // hours
                        (int)floor(seconds/60),             // minutes
                        (int)sec,                           // seconds
                        (int)floor((seconds - sec) * 1000)  // milliseconds
             ];
}

// NOTE: %02d is C style formatting where:
// % - the usual suspect
// 02 - target length (pad single digits for padding)
// d - the usual suspect

有关此格式的更多信息,请参阅此讨论

于 2013-11-19T12:03:43.207 回答
0

这是一个相当容易重新利用的 Processing/Java 等价物。

String timecodeString(int fps) {
  float ms = millis();
  return String.format("%02d:%02d:%02d+%02d", floor(ms/1000/60/60),    // H
                                              floor(ms/1000/60),       // M
                                              floor(ms/1000%60),       // S
                                              floor(ms/1000*fps%fps)); // F
}
于 2011-09-18T21:09:43.447 回答