iphone - 计时器在模拟器和设备上的行为不同

标签 iphone ios-simulator nstimer

我的问题如下:

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

我在 iPhone 上使用了这 5 个计时器来移动帧。但 timer3timer4 在 iPod 和模拟器上的行为不同。 timer3timer4 在 iPod 上比我想要实现的要慢,并且在模拟器上运行良好。

请指出问题出在哪里?

最佳答案

NSTimers 无法安排 2 个或更多方法同时启动(特别是在只有 1 个处理器核心的 iOS 设备上)。如果第一个计时器任务很慢,第二个计时器任务就会迟到。

在模拟器上,第一个任务的运行速度可能会快 10 倍或更多(由于原始 CPU 和内存性能),从而使第二个任务的延迟程度大大降低,以至于您不会注意到它已晚了。

要么让每个任务更快,要么调整计时器,以便任务不会重叠。或者,如果每个计时器任务发生在某个最不常见的多个时间段,则将其内部完成的操作组合起来。

关于iphone - 计时器在模拟器和设备上的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3616509/

相关文章:

swift - 如何停止来自其他 viewController 的计时器

ios - Swift 为什么我的 NSTimer 不能均匀触发?

iOS 在 600 秒后杀死我的后台任务

iphone - 是什么导致应用程序在后台运行很长时间后在恢复时崩溃?

ios - 如何构建支持i386和armv7的ios框架

xcode - iOS 9 iPhone 4s 和 5 上不显示图像

ios - Keychain Services API 在 iphonesimulator 6.0 中因 errSecNotAvailable 而失败

iphone - 如何伪造内置应用程序切换动画?

javascript - 在不应该的情况下在任何 Iphone 浏览器中触发动画

iphone - 我应该购买一台 Mac 来开发或测试我的 iPhone Web 应用程序吗?