从背景NSThread绘制UIView?

时间:2009-08-28 01:57:57

标签: iphone opengl-es nsthread

我正在开发GL涂料应用程序。

要绘制任何对象,我使用的是实现的UIView。

开始绘画方法:

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

但它不起作用(不绘制对象)。

这里有什么问题?

请帮帮我们。

提前致谢。

1 个答案:

答案 0 :(得分: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