2010-11-18 14 views
3

setSelectorOnMainThread kullanarak setNeedsDisplayInRect çağrısı nasıl yapılır? Sorun rect. Rect'i performSelectorOnMainThread yönteminde nasıl geçireceğimi bilmiyorum. Bu yöntem NSObject soruyor, ancak CGRect NSObject değil, sadece yapı *.setSelectorOnMainThread kullanarak setNeedsDisplayInRect çağrısı nasıl yapılır?

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

-(void)drawRect:(CGRect)rect { 

    /// drawing... 

} 

Ben Ana değil Konuya dan MainThread içinde setNeedsDisplayInRect yöntemi çağırmak gerekir. Bunu yapmayı bilen var mı ?????????? Şimdiden teşekkürler.

Gerçekten teşekkürler.

cevap

4

Eğer iOS 4.0 veya daha yeni iseniz, iOS 3.2 üzerinde aşağıdaki

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

kullanabilir ve daha önceki, bir NSInvocation ayarlayabilir ve ana iş parçacığı üzerinde o çalıştırın:

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]; 

Devam etmeden önce bu aramanın tamamlanmasını beklemeniz gerekmedikçe waitUntilDone değerini HAYIR olarak ayarlamak isteyebilirsiniz.

İlgili konular