取消排队的性能选择器: 滞后调用

有人知道在调用 performSelector:withObject:afterDelay时,是否可以从事件堆栈或计时器堆栈(或 API 使用的任何机制)中取消已经排队的选择器事件?

我使用这个事件堆栈来修改 TabBar 选项卡中图像的属性,有时会在一个快速执行的循环中排队长达10秒的更改... 也许是5毫秒左右。

如果用户切换标签,问题就出现了... 比如我有一个图像修改队列,一旦标签 # 4被启用,然后用户迅速切换到标签 # 3,然后马上回到标签 # 4... 这将重新排队另外10秒钟的修改值,而旧队列仍然在播放,如果切换得足够快,可能2或3秒进入队列... 但即使到达5秒进入流也是一个问题。

因此,我需要一些方法来取消旧堆栈的更改,然后再将新堆栈放到..。

I'm writing this query in the past tense because I already came up with an alternative solution to this problem by adding a hawk-eyed event filter on the playback function. however I am still curious if event cancellation is possible, because I have a feeling such knowledge will come in handy in the future. thank you for any assistance rendered :)

34786 次浏览
[NSObject cancelPreviousPerformRequestsWithTarget:]

或者

[NSObject cancelPreviousPerformRequestsWithTarget:selector:object:]

The target is the original object on which performSelector:afterDelay: was called.

例如:

// schedule the selector
[self performSelector:@selector(mySel:) withObject:nil afterDelay:5.0];
// cancel the above call (and any others on self)
[NSObject cancelPreviousPerformRequestsWithTarget:self];

看看 苹果医生,它就在 performSelector:withObject:afterDelay:描述的最后。

Check the NSRunLoop docs. You want -cancelPerformSelectorsWithTarget:

If you are looking for "performSelector" to have its matching "cancelPreviousPerformSelector"... it doesn't. (Ugh, Apple, why do you do that to me???)

呃,啊,这个配对的方法是,

performSelector


cancelPreviousPerformRequestsWithTarget

(只是为了在不搜索文档的情况下更难记住。)

为了取消以前所有的执行请求,可以使用:

[NSObject cancelPreviousPerformRequestsWithTarget:self];