如何停止计时器触发的运行循环?

发布于 2024-09-10 13:59:59 字数 836 浏览 1 评论 0原文

如果我设置了这样的运行循环:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走走停停 2024-09-17 13:59:59

您需要向计时器发送无效消息以将其从 RunLoop 中删除。

参见文档

[anyTimer invalidate];

You need to send a invalidate message to the timer to remove it from the RunLoop.

See Doc

[anyTimer invalidate];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文