微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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];

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐