objective-c 取消排队的 performSelector:afterDelay 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1806445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:33:32  来源:igfitidea点击:

cancelling queued performSelector:afterDelay calls

objective-cselector

提问by eerok512

does anybody know if it is possible to cancel already queued selector events from the event stack or timer stack (or whatever mechanism it is that is utilized by the API) when you call performSelector:withObject:afterDelay?

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

I was using this event stack to alter the attributes of an image within a TabBar tab, and would sometimes queue up to 10 seconds worth of changes in one quickly executed for loop... maybe 5 milliseconds or so.

我正在使用此事件堆栈来更改 TabBar 选项卡中图像的属性,并且有时会在一个快速执行的 for 循环中排队长达 10 秒的更改……可能需要 5 毫秒左右。

the problem arises if the user switches tabs... like say I have the image alterations queued for an image that is displayed as soon as Tab #4 is enabled, and then the user quickly switches to Tab #3 and then right back to Tab #4... this would then re-queue another 10 seconds worth of alterations while the old queue was still playing, probably around 2 or 3 seconds in to the queue if switched quick enough... but even arriving at 5 seconds in to the stream was a problem.

如果用户切换选项卡,问题就会出现......就像说我有图像更改排队等待启用选项卡 #4 后立即显示的图像,然后用户快速切换到选项卡 #3,然后又回到选项卡#4...这将在旧队列仍在播放时重新排队另外 10 秒的更改,如果切换得足够快,可能会在队列中大约 2 或 3 秒......但即使到达 5 秒流是一个问题。

so I needed some way to cancel the old stack of changes before putting a new stack on...

所以我需要一些方法来取消旧的更改堆栈,然后再放置新堆栈...

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 :)

我用过去时写这个查询是因为我已经想出了一个替代解决方案来解决这个问题,方法是在播放函数上添加一个鹰眼事件过滤器。但是我仍然很好奇是否可以取消活动,因为我觉得这些知识将来会派上用场。感谢您提供的任何帮助:)

回答by stefanB

[NSObject cancelPreviousPerformRequestsWithTarget:]

or

或者

[NSObject cancelPreviousPerformRequestsWithTarget:selector:object:]

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

target是其原始对象performSelector:afterDelay:被调用。

For example:

例如:

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

See apple docs, it's right at the end of the performSelector:withObject:afterDelay:description.

请参阅apple docs,它就在performSelector:withObject:afterDelay:说明的末尾。

回答by Irene

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

如果您正在寻找“performSelector”以使其匹配“ cancelPreviousPerformSelector”......它没有。(呃,苹果,你为什么要这样对我???)

The, er, ah, "matching" methods are:

呃,啊,“匹配”的方法是:

performSelector

cancelPreviousPerformRequestsWithTarget

(Just to make it extra hard to remember, without searching the docs.)

(只是为了让它更加难以记住,而无需搜索文档。)

回答by Marco Mirisola

In order to cancel all previous perform requests, you may use :

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

[NSObject cancelPreviousPerformRequestsWithTarget:self];   

回答by NSResponder

Check the NSRunLoop docs. You want -cancelPerformSelectorsWithTarget:

检查 NSRunLoop 文档。你要-cancelPerformSelectorsWithTarget: