我非常接近完成我的第一个 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]];
}