如何停止计时器触发的运行循环?
如果我设置了这样的运行循环:
NSRunloop* loop = [NSRunloop currentRunLoop];
[runLoop addTimer:anyTimer forMode:NSDefaultRunLoopMode];
我可以再次停止它吗? 或者是忽略我用来触发进一步操作的通知的唯一方法?
好的,我举一个问题的例子:
-(void)blinkeffekt:(double)pollingTime{
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
if (pollingTime != 0) {
NSTimeInterval interval =(double)pollingTime / 1000;
NSTimer timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(polling) userInfo:nil repeats:YES];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
}
else {
[timer invalidate];
}
}
当然,这里有几个错误 - 毫无疑问。但我认为,这表明了我的问题。到目前为止,这个问题的答案还无法真正解决。
我需要运行一个计时器并稍后停止。最好是在班级的另一个职能之外。 但后来我无法再访问“计时器”,并且 runloop 不允许弄清楚这样的“消息”是否可用。 如果我为该函数的每次调用注册一个新的计时器,那将是极其无效的。
if i set up a runloop like that:
NSRunloop* loop = [NSRunloop currentRunLoop];
[runLoop addTimer:anyTimer forMode:NSDefaultRunLoopMode];
can i stop it again ?
or is the only way to ignore the notification i use to trigger the further action ?
ok, i give an example for the problem:
-(void)blinkeffekt:(double)pollingTime{
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
if (pollingTime != 0) {
NSTimeInterval interval =(double)pollingTime / 1000;
NSTimer timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(polling) userInfo:nil repeats:YES];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
}
else {
[timer invalidate];
}
}
surely, here are several errors - no question. but i think, it shows my problem. and this is not really solvable with the answers until now.
i need to run a timer and stop it later. and ideally out of another function in the class.
but then i cannot access "timer" anymore and runloop doesnt allow to figure out, if such a "message" is available.
and it would be extremely ineffective, if i would register for each calling of the function a new timer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要向计时器发送无效消息以将其从 RunLoop 中删除。
参见文档
You need to send a invalidate message to the timer to remove it from the RunLoop.
See Doc