3

performSelectorOnMainThreadを使用してsetNeedsDisplayInRectを呼び出す方法は? 問題はレクトです。performSelectorOnMainThread メソッドで rect を渡す方法がわかりません。このメソッドは NSObject を要求しますが、CGRect は NSObject ではなく、単なる構造体 *.

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

-(void)drawRect:(CGRect)rect {

    /// drawing...

}

Main Thread ではなく、Main Thread で 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 に答える