objective-c – 使用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.

有什么建议?

解决方法

如果你的计时器以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];

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...