2010-11-18 3 views
3

performSelectorOnMainThread를 사용하여 setNeedsDisplayInRect를 호출하는 방법은 무엇입니까? 문제는 해결됩니다. performSelectorOnMainThread 메서드에서 rect를 전달하는 방법을 모르겠습니다. 이 메서드는 NSObject를 요청하지만 CGRect는 NSObject가 아니며 단지 구조체 *입니다.performSelectorOnMainThread를 사용하여 setNeedsDisplayInRect를 호출하는 방법은 무엇입니까?

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

-(void)drawRect:(CGRect)rect { 

    /// drawing... 

} 

메인 스레드가 아닌 MainThread에서 setNeedsDisplayInRect 메소드를 호출해야합니다. 아무도 그것을하는 방법을 알고 있습니까 ?????????? 미리 감사드립니다.

정말 고마워요.

답변

4

당신이 아이폰 OS 4.0 이상이 있다면, 당신은 아이폰 OS 3.2에 다음과 같은

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self setNeedsDisplayInRect:theRect]; 
}); 

을 사용할 수 있습니다 및 이전 버전은있는 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로 설정할 수 있습니다.

관련 문제