我非常接近完成我的第一个iphone应用程序,它一直是一种快乐。我试图使用当前时间通过NSTimer在UILabel上显示当前时间(NSDate)来添加运行时间码。 NSDate对我来说工作正常,显示小时,分钟,秒,毫秒。但是,而不是毫秒,我需要显示每秒24帧。使用NSTimer和NSDateFormatter显示时间码
问题是我需要每秒的帧数与小时,分钟和秒同步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]];
}
经过一番玩,我仍然不知道如何实现这一点。我已经能够成功地做你的建议,但似乎我错过了实际上每秒24帧转换的一大块数学。我不需要每秒显示100毫秒,而是需要显示每秒钟通过0-23的数字,并且我需要它与NSTimer完美同步,以便实际完成并在每秒结束时重新启动。 – 2010-05-18 05:10:05