我写了一个使用MKMapView的应用程序。此应用程序使用计时器更新屏幕上的某些信息。实际上,当用户触摸地图并开始拖动时,在用户松开触摸之前不再触发计时器。我注意到使用新的iOS 6,这个问题就消失了。但是我还需要支持iOS 5.我还没弄清楚是否只有定时器没有被触发或者根本没有处理任何事件。有什么想法吗?
答案 0 :(得分:1)
好的,我在这里找到了解决方案:UIScrollView pauses NSTimer until scrolling finishes
基本上你必须把NSTimer放在它自己的运行循环中。
答案 1 :(得分:0)
嗯,这表明定时器和触摸处理代码正在由同一个runloop处理,或者可能是触摸正在阻塞,所以当定时器完成代码试图运行时,它不能。尝试使用带有完成处理程序的异步块来运行计时器。
- (void)startTimerInBackground {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
//Start timer here, set completion method to be called
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 2.0
target: self
selector:@selector(timerCompletionMethod:)
userInfo: nil repeats:NO];
});
}
- (void)timerCompletionMethod:(NSTimer *)timer {
//Switch back to main thread here for completion code
dispatch_async(dispatch_get_main_queue(), ^(void) {
});
}
看看是否有帮助,请注意虽然计时器不可靠,如果你需要非常准确的计时,你应该看看替代品,这里有一些非常好的信息: