我知道有关此主题的许多问题,因为我自己之前也问过一个问题,但是,我的问题现在似乎与线程部分更相关。我有以下2种方法。
-(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];
当发生这种情况时,似乎会创建多个计时器,因此我的昏暗函数被调用得太频繁,从而使单个计时器看起来运行得非常快?使用辅助线程启动和重新启动计时器的最佳方法是什么?请注意,此问题似乎仅在重复快速按下“跳过”按钮时才会发生,而如果只是偶尔按下则可以正常工作?
有人有主意吗?非常感谢
Jules
您可以使用布尔值来了解计时器是否正在运行。当您启动计时器时,您将其设置为 true,当您停止计时器时,您将其设置为 false。当恢复定时器函数被调用时,你检查这个变量,如果它是真的,你就不会启动一个新的定时器。
另一种解决方案是限制用户与按钮的交互。如果按下该按钮,您将使其暂时处于非活动状态。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)