objective-c – 使用NSTimer显示每秒帧数的时间码
发布时间:2020-12-16 07:25:47 所属栏目:百科 来源:网络整理
导读:我正在开发一款需要显示运行时间码时钟的iPhone / iPad应用程序.我已经让它显示正确的小时,??分??钟和秒,使用此代码没有问题: - (void) viewDidLoad { // Start the Timer method for here to start it when the view loads. runTimer = [NSTimer scheduled
我正在开发一款需要显示运行时间码时钟的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. 有什么建议? 解决方法
如果你的计时器以0.01秒的周期运行,那么它的频率是100帧/秒(好吧,最好说它每秒有100个函数调用).但是如果你需要显示精确的时间段(因为有时可能会延迟定时器),那么你需要存储以前的通话日期然后再使用
NSDate* new_date = [NSDate date]; double freq = 1.0 / [new_date timeIntervalSinceDate: old_date]; [old_date release]; old_date = [new_date retain]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |