2009-08-28 116 views
-1

我正在開發GL油漆應用程序。從背景NSThread繪製UIView?

要繪製任何對象,我正在使用實現的UIView。

開始paint方法:

- (void)viewDidLoad { 
    .... 
    [NSThread detachNewThreadSelector:@selector(paintingObjects) 
          toTarget:self 
          withObject:nil]; 
} 

- (void)paintingObjects { 
    while(1) { 
     [NSThread sleepForTimeInterval:2]; 
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
     ImplementedView *tmp = [self view]; 
     [tmp draw]; 
     [pool drain]; 
    } 
} 

但它無法正常工作(不噴漆的對象)。

這裏有什麼問題?

請幫幫我吧。

在此先感謝。

+0

據我所知,UI對象不是線程安全的,但我坦率地承認我可能是錯的。 – 2009-08-28 02:00:05

回答

1

所有的用戶界面類線程安全的,必須從主線程調用:

- (void)paintingObjects { 
    while(1) { 
     [NSThread sleepForTimeInterval:2]; 
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
     ImplementedView *tmp = [self view]; 
     [tmp performSelectorOnMainThread:@selector(draw) withObject:nil waitUntilDone:YES]; 
     [pool drain]; 
    } 
} 

在這種情況下,你可能會更好使用NSTimer

+0

不應該是performSelectorOnMainThread? – fbrereto 2009-08-28 16:28:33

+0

D'oh!是的,它應該是 - 這就是我在Windows上提供Mac解答的答案。變了! – rpetrich 2009-08-28 17:21:44