2010-11-18 69 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,除非您在繼續之前絕對需要等待此呼叫完成。

相關問題