计时器在模拟器和设备上的行为不同
我的问题如下:
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0/5 target:self
selector:@selector(Loop1) userInfo:nil repeats:YES];
timer2 = [NSTimer scheduledTimerWithTimeInterval:1.0/5 target:self
selector:@selector(Loop2) userInfo:nil repeats:YES];
timer3 = [NSTimer scheduledTimerWithTimeInterval:1.0/20 target:self
selector:@selector(Loop3) userInfo:nil repeats:YES];
timer4 = [NSTimer scheduledTimerWithTimeInterval:1.0/10 target:self
selector:@selector(Loop4) userInfo:nil repeats:YES];
timer5 = [NSTimer scheduledTimerWithTimeInterval:1.0/5 target:self
selector:@selector(Loop5) userInfo:nil repeats:YES];
我已经使用这 5 个计时器在 iPhone 上移动帧。但 timer3
和 timer4
在 iPod 和模拟器上的行为不同。 timer3
和 timer4
在 iPod 上比我想要实现的要慢,并且在模拟器上运行良好。
请指教问题出在哪里?
My problem is as below:
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0/5 target:self
selector:@selector(Loop1) userInfo:nil repeats:YES];
timer2 = [NSTimer scheduledTimerWithTimeInterval:1.0/5 target:self
selector:@selector(Loop2) userInfo:nil repeats:YES];
timer3 = [NSTimer scheduledTimerWithTimeInterval:1.0/20 target:self
selector:@selector(Loop3) userInfo:nil repeats:YES];
timer4 = [NSTimer scheduledTimerWithTimeInterval:1.0/10 target:self
selector:@selector(Loop4) userInfo:nil repeats:YES];
timer5 = [NSTimer scheduledTimerWithTimeInterval:1.0/5 target:self
selector:@selector(Loop5) userInfo:nil repeats:YES];
I have used these 5 timers to move frames on iPhone. But timer3
and timer4
behave differently on iPod and simulator. timer3
and timer4
are slower on iPod than what I want to implement, and works fine on simulator.
Please suggest where the problem is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NSTimers 无法安排 2 个或更多方法同时启动(特别是在只有 1 个处理器核心的 iOS 设备上)。如果第一个计时器任务很慢,第二个计时器任务就会迟到。
在模拟器上,第一个任务的运行速度可能会快 10 倍或更多(由于原始 CPU 和内存性能),从而使第二个任务的延迟程度大大降低,以至于您不会注意到它已延迟。
要么让每个任务更快,要么调整计时器,使任务不会重叠。或者,如果每个计时器任务发生在某个最不常见的多个时隙,则将其内部完成的操作组合起来。
NSTimers cannot schedule 2 or more methods to start at the same time (especially on an iOS device with only 1 processor core). If the first timer task is slow, the second one will be late.
On the Simulator, the first task may well run 10X or more faster (due to raw CPU and memory performance) making the second task so much less late that you don't notice that it is late.
Either make each task faster, or skew the timers so that the tasks don't overlap. Or combine what's done inside each timer task, if that task occurs at some least common multiple time slot.
您的问题可能只是性能问题。使用活动监视器工具和设备上运行的应用程序检查设备是否耗尽了资源。
我的猜测是,您要求设备每十分之一秒重复三个操作,这可能会超出其容量,具体取决于操作。
也许您可以删除 Loop3 和 Loop4 的代码行?
Your problem might just be a performance issue. Check using the Activity Monitor Tool and the application running on the device if the device doesn't just run out of resources.
My guess is that you're asking the device to repeat three operations every 10th of a second and that might be out of its capacity depending on the operation.
Maybe you could drop the lines of code of Loop3 and Loop4 ?
如果您需要高精度,则不能依赖计时器。这将取决于设备负载。模拟器在Mac上运行,因此您将获得更高的精度。但该设备的功能并不比 Mac 弱。所以会有明显的延迟。
为什么需要这样的定时器?您是否正在尝试将某些东西制作成动画?在这种情况下,有更好的选择。特别研究 UIView 动画而不是计时器。
You can not rely on timer if you require high precision. This will depend on device load. The simulator is running on Mac, so you will get higher precision. But the device is less less powerful than the Mac. So there will be obvious delay.
Why do you need timers in this way? Are you trying to animate something? In that case there are better options available. Specially look into UIView animation instead of timer.
根据文档: http ://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
计时器时间间隔的有效分辨率限制为大约 50-100 毫秒
你的两个麻烦的计时器是 1/10 和 1/20 秒,恰好是 100 和 50 毫秒。您正处于 NSTimer 可以处理的最佳时间段上,并且您应该预料到一些不可靠的情况。
According to the docs: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds
Your two troublesome timers are 1/10 and 1/20 of a second, which happen to be exactly 100 and 50 milliseconds. You're flirting with the finest slice of time an NSTimer can handle, and you should expect some unreliability.