使用 NSTimer 显示每秒帧数时间码
我正在开发一个 iPhone/iPad 应用程序,需要显示正在运行的时间码时钟。我已经使用此代码毫无问题地显示正确的小时、分钟和秒:
- (void) viewDidLoad {
// Start the Timer method for here to start it when the view loads.
runTimer = [NSTimer scheduledTimerWithTimeInterval: .01 target: self selector: @selector(updateDisplay) userInfo: nil repeats: YES];
}
- (void)updateDisplay {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *date = [NSDate date];
// Display each hour, minute, second and frame.
[formatter setDateFormat:@"hh"];
[timecodeHourLabel setText:[formatter stringFromDate:date]];
[formatter setDateFormat:@"mm"];
[timecodeMinuteLabel setText:[formatter stringFromDate:date]];
[formatter setDateFormat:@"ss"];
[timecodeSecondLabel setText:[formatter stringFromDate:date]];
}
问题是当我需要显示每秒帧数时。我知道计算 1/24 * 1000
可以得出一帧中有多少毫秒。我只是不知道如何使 NSDate
和 NSTimer
函数与此代码配合使用,并允许它根据需要尽快更新 UILabel
用于运行时间码。
有什么建议吗?
I am working on an iPhone/iPad app that needs to display a running timecode clock. I have gotten it to display the correct hours, minutes, and seconds with no problem using this code:
- (void) viewDidLoad {
// Start the Timer method for here to start it when the view loads.
runTimer = [NSTimer scheduledTimerWithTimeInterval: .01 target: self selector: @selector(updateDisplay) userInfo: nil repeats: YES];
}
- (void)updateDisplay {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *date = [NSDate date];
// Display each hour, minute, second and frame.
[formatter setDateFormat:@"hh"];
[timecodeHourLabel setText:[formatter stringFromDate:date]];
[formatter setDateFormat:@"mm"];
[timecodeMinuteLabel setText:[formatter stringFromDate:date]];
[formatter setDateFormat:@"ss"];
[timecodeSecondLabel setText:[formatter stringFromDate:date]];
}
The issue is when I need to display frames per second. I know that calculating 1/24 * 1000
gives me how many milliseconds are in one frame. I just don't know how to make the NSDate
and NSTimer
functions work with this code and allow it to update a UILabel
as quickly as needed for running timecode.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你的计时器以 0.01 秒的周期运行,那么它的频率就是 100 帧/秒(好吧,最好说它每秒有 100 个函数调用)。但如果你需要显示精确的时间段(因为有时定时器可能会延迟),那么你需要存储上一次调用日期,然后使用
If your timer is running with the period of 0.01 sec, then it's frequency is 100 frames/sec (well, it's better to say it has 100 function calls per second). But if you need to display the precise time period (cause sometimes the timer may be delayed), then you need to store the previous call date and then use
这是一个处理/Java 等效项,可以非常简单地重新调整用途。
Here's a Processing/Java equivalent that's fairly straightforward to repurpose.