-1

我正在开发 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];
    }
}

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

这里有什么问题?

请帮帮我。

提前致谢。

4

1 回答 1

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.

于 2009-08-28T16:24:03.927 回答