我的问题如下:
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 个计时器来移动帧。但 timer3
和 timer4
在 iPod 和模拟器上的行为不同。 timer3
和 timer4
在 iPod 上比我想要实现的要慢,并且在模拟器上运行良好。
请指出问题出在哪里?
最佳答案
NSTimers 无法安排 2 个或更多方法同时启动(特别是在只有 1 个处理器核心的 iOS 设备上)。如果第一个计时器任务很慢,第二个计时器任务就会迟到。
在模拟器上,第一个任务的运行速度可能会快 10 倍或更多(由于原始 CPU 和内存性能),从而使第二个任务的延迟程度大大降低,以至于您不会注意到它已晚了。
要么让每个任务更快,要么调整计时器,以便任务不会重叠。或者,如果每个计时器任务发生在某个最不常见的多个时间段,则将其内部完成的操作组合起来。
关于iphone - 计时器在模拟器和设备上的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3616509/