2010-11-18 44 views
3

如何使用performSelectorOnMainThread调用setNeedsDisplayInRect?问题是直接的。我不知道如何在performSelectorOnMainThread方法中传递矩形。这个方法要求NSObject,但CGRect不是NSObject,它只是结构*。如何使用performSelectorOnMainThread调用setNeedsDisplayInRect?

//[self setNeedsDisplayInRect:rect]; 
[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:0 waitUntilDone:YES]; 
} 

-(void)drawRect:(CGRect)rect { 

    /// drawing... 

} 

我需要从Main Thread中调用MainThread中的setNeedsDisplayInRect方法。 有谁知道该怎么做??????????在此先感谢..

真的谢谢。

回答

4

如果你在iOS 4.0或更高版本,可以使用下面的

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self setNeedsDisplayInRect:theRect]; 
}); 

在iOS 3.2及更早版本,你可以建立一个NSInvocation的,并运行在主线程:

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(setNeedsDisplayInRect:)]]; 
[invocation setTarget:self]; 
[invocation setSelector:@selector(setNeedsDisplayInRect:)]; 
// assuming theRect is my rect 
[invocation setArgument:&theRect atIndex:2]; 
[invocation retainArguments]; // retains the target while it's waiting on the main thread 
[invocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:YES]; 

您可能需要将waitUntilDone设置为NO,除非您在继续之前绝对需要等待此呼叫完成。

相关问题