如何“验证”一个NSTimer后失效呢? [英] How to "validate" a NSTimer after invalidating it?

查看:110
本文介绍了如何“验证”一个NSTimer后失效呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上,我有这个定时器,当它收到一个键事件时应该重复,并且在用户释放键时失效。但是,即使通过调用NSRunLoop中的 addTimer:forMode:,我也无法验证计时器。有人知道为什么这是发生,我如何可以解决这个问题?感谢。

So basically, I have this timer that should be repeated when it receives a key event, and invalidates when the user releases the key. However, I am unable to "validate" the timer back even by calling the addTimer:forMode: in NSRunLoop. Does anyone know why this is happening and how I can fix this? Thanks.

推荐答案

您无法使用无效的计时器。

You cannot use invalidated timer.

文档:


一旦在运行循环上计划,定时器将以指定的
时间间隔触发,直到失效。非重复计时器在触发后立即使
本身无效。但是,对于重复的计时器,
必须通过调用其无效的
方法自己使计时器对象无效。调用此方法请求从
当前运行循环中移除定时器;因此,应该总是从安装了定时器的同一个线程调用invalidate
方法。
使计时器无效将立即禁用它,使其不再
影响运行循环。运行循环然后删除并释放
定时器,或者在invalidate方法返回之前,或者在某些
以后的点。一旦无效,计时器对象就无法重复使用。

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes and releases the timer, either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.

这篇关于如何“验证”一个NSTimer后失效呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆