如何使用performSelectorOnMainThread调用setNeedsDisplay Rect?

时间:2010-11-18 22:03:28

标签: iphone setneedsdisplayinrect

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

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

-(void)drawRect:(CGRect)rect {

    /// drawing...

}

我需要在主线程中调用MainThread中的setNeedsDisplayInRect方法。 有谁知道怎么做??????????提前谢谢..

非常感谢。

1 个答案:

答案 0 :(得分: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,除非您在继续操作之前绝对需要等待此调用完成。