如何使用performSelectorOnMainThread调用setNeedsDisplayInRect?问题是rect。我不知道如何在performSelectorOnMainThread方法中传递rect。这个方法询问NSObject,但是CGRect不是结构,它只是NSObject *。
//[self setNeedsDisplayInRect:rect];
[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:0 waitUntilDone:YES];
}
-(void)drawRect:(CGRect)rect {
/// drawing...
}我需要从非主线程调用MainThread中的setNeedsDisplayInRect方法。有人知道怎么做吗?提前谢谢..
真的谢谢你。
发布于 2010-11-19 07:04:21
如果您使用的是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,除非您绝对需要等待此调用完成后才能继续。
https://stackoverflow.com/questions/4220048
复制相似问题