带有多个参数的 iOS performSelectorOnMainThread [英] iOS performSelectorOnMainThread with multiple arguments
问题描述
我想从另一个线程在主线程上执行一个选择器,但是选择器有多个参数,类似于:
I would like to perform a selector on the main thread from another thread, but the selector has multiple arguments, similar to this:
-(void) doSomethingWith:(int) a b:(float)b c:(float)c d:(float)d e:(float)e {//...}
如何使用 performSelectorOnMainThread: withObject: waitUntilDone:
实现此功能?
How can I get this working with performSelectorOnMainThread: withObject: waitUntilDone:
?
编辑
我想解释一下为什么我需要这个.
I would like to explain why i need this.
我在主线程上使用 UIImageViews,我在另一个线程上为它们进行计算.我使用了很多计算,所以如果我在主线程上做所有事情,应用程序就会滞后.我知道 UI 元素只能在主线程上操作,这就是为什么我希望它以这种方式工作,因此主线程可以无延迟地监听触摸事件.
I'm working with UIImageViews on the main thread, and I make the calculations for them on another thread. I use a lot of calculations so if i make everything on the main thread, the app lags. I know that UI elements can only be manipulated on the main thread, this is why i would like it to work this way, so the main thread can listen to touch events without lags.
推荐答案
当您使用 iOS >= 4 时,您应该这样做:
When you're using iOS >= 4, you'd do this instead:
dispatch_async(dispatch_get_main_queue(), ^{
[self doSomething:1 b:2 c:3 d:4 e:5];
});
这就像在做 waitUntilDone:NO
.如果您想等到方法完成,请改用 dispatch_sync
.
That's like doing waitUntilDone:NO
. If you want to wait until the method is finished, use dispatch_sync
instead.
这篇关于带有多个参数的 iOS performSelectorOnMainThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!