我知道关于这个主题的很多问题,正如我之前已经问过一个问题,我现在的问题似乎与线程部分有关。我有以下两种方法。
-(void) restartTimer {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.
target:self
selector:@selector(dim)
userInfo:nil
repeats:YES];
time = 31;
NSLog(@"calling restart timer");
[self performSelectorOnMainThread:@selector(timerImageUpdate) withObject:nil waitUntilDone:NO];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
[pool drain];
}
-(void) resumeTimer {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.
target:self
selector:@selector(dim)
userInfo:nil
repeats:YES];
NSLog(@"calling resume timer");
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
[pool drain];
}
游戏开始时会调用restartTimer函数。这很好用,计时器可以很好地激活暗淡选择器。当用户快速连续单击我的跳过按钮时,会出现问题。 [skip]更新MKMapView并在委托方法mapdidfinishloading中调用以下内容:
[NSThread detachNewThreadSelector:@selector(restartTimer)toTarget:self withObject:nil];
当发生这种情况时,似乎创建了几个定时器,因此我的调光功能太经常被调用,使得单个定时器的运行速度非常快?使用辅助线程启动和重启计时器的最佳方法是什么?请注意,如果反复快速按下跳过按钮,似乎只会发生此问题,而如果只是一次又一次地按下它会正常工作吗?
有人有什么想法吗?非常感谢
朱
答案 0 :(得分:0)
您可以使用bool来了解您是否正在运行计时器。当您启动计时器时,将其设置为true,当您停止计时器时将其设置为false。当调用恢复计时器功能时,检查此变量,如果是,则不启动新计时器。
另一种解决方案是限制用户与按钮的交互。如果按下按钮,则会使其暂停一段时间。