线程 NSTimer
我知道有关此主题的许多问题,因为我自己之前也问过一个问题,但是,我的问题现在似乎与线程部分更相关。我有以下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];
当发生这种情况时,似乎会创建多个计时器,因此我的昏暗函数被调用得太频繁,从而使单个计时器看起来运行得非常快?使用辅助线程启动和重新启动计时器的最佳方法是什么?请注意,此问题似乎仅在重复快速按下“跳过”按钮时才会发生,而如果只是偶尔按下则可以正常工作?
有人有什么想法吗?非常感谢
朱尔斯
I am aware of the many questions regarding this topic, as I myself have asked one previously however, my issue now seems to be more related to the threading part. I have the following 2 methods.
-(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];
}
The restartTimer function is called when the game begins. This works fine and the timer fires the dim selector nicely. The problem occurs when the user clicks my skip button in quick succession. [skip] updates an MKMapView and in the delegate method mapdidfinishloading the follwing is called :
[NSThread detachNewThreadSelector:@selector(restartTimer) toTarget:self withObject:nil];
When this happens it seems several timers are created and thus my dim function is called far too often giving the appearance of the single timer running really fast? What is the best way to start and restart the timer using a secondary thread? Note this problem only seems to happen if the skip button is pressed repeatedly and quickly, while it works fine if just pressed now and again?
Anyone have any ideas? Many thanks
Jules
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用布尔值来了解计时器是否正在运行。当您启动计时器时,您将其设置为 true,当您停止计时器时,您将其设置为 false。当恢复定时器函数被调用时,你检查这个变量,如果它是真的,你就不会启动一个新的定时器。
另一种解决方案是限制用户与按钮的交互。如果按下该按钮,您将使其暂时处于非活动状态。
You can use a bool to know if you have a timer running or not. When you start the timer you set it to true, when you stop the timer you set it to false. When resume timer function is called you check this variable and if it's true you do not start a new timer.
Another solution would be to limit user interaction with the button. If the button is pressed you make it inactive for a time.