从背景NSThread绘制UIView?
问题描述:
我正在开发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];
}
}
但它无法正常工作(不喷漆的对象)。
这里有什么问题?
请帮帮我吧。
在此先感谢。
答
所有的用户界面类不线程安全的,必须被从主线程调用:
- (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
。
据我所知,UI对象不是线程安全的,但我坦率地承认我可能是错的。 – 2009-08-28 02:00:05