3

如何使用 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 方法。有谁知道怎么做吗?????????提前致谢..

真的感谢。

4

1 回答 1

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,除非您绝对需要等待此调用完成后再继续。

于 2010-11-18T23:04:21.260 回答