2009-08-28 119 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