如何在ios中获得准确的计时器 [英] how to get a accurate timer in ios

查看:88
本文介绍了如何在ios中获得准确的计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个需要计时器的应用程序。我尝试过 NSTimer ,但 NSTimer 总是丢失或延迟。 iOS SDK中是否有可以准确记录时间的类。精度在20ms到40ms之间是可以的。我希望能够在固定的时间间隔内更改图像。

I am doing an app which needs a timer. I have tried NSTimer but NSTimer always loses or gets delayed. Is there a class in the iOS SDK which can log time accurately. An accuracy between 20ms and 40ms is okay. I'd like to be able to change an image in a fixed time interval.

NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval:.04f target:self    selector:@selector(timer1) userInfo:nil repeats:YES];
- (void)timer1
{
   NSLog(@"timer1 timer %@",[NSDate date]);
}


推荐答案

NSTimer 不是高分辨率计时器。来自 NSTimer 类文档:

NSTimer is not a high-resolution timer. From the NSTimer class documentation:


由于各种输入源的典型运行循环管理,定时器的时间间隔的有效分辨率限制在50-100毫秒的数量级。

Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds.

此外要链接到@CJ Foley,你可以查看 dispatch_after 函数。

In addition to link suggested by @CJ Foley, you can check out dispatch_after function.

这篇关于如何在ios中获得准确的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆