我是iPhone开发的新手。我有一个游戏循环设置如下。

(void)CreateGameTick:(NSTimeInterval) in_time
{
  [NSThread detachNewThreadSelector:@selector(GameTick) toTarget:self withObject:nil];
}

我的基本游戏刻度/渲染看起来像这样

(void)GameTick
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  CGRect wrect = [self bounds];

  while( m_running )
  {
    [self drawRect: wrect];
  }

  [pool release];       
}

我的渲染函数被调用。然而,没有任何东西被绘制(我使用Core Graphics在派生的UIView上绘制一些线条)。

如果我通过计时器调用我的更新,那么一切都很好。

你能告诉我为什么渲染会在通过线程完成时失败吗?是否可以通过线程使其工作?

由于 富

有帮助吗?

解决方案

你不能(嗯,不应该)直接调用-drawRect:相反,使用-setNeedsDisplay;然后,您的视图将在下次通过事件循环时更新。如果你在一个单独的线程中运行它,你可能需要使用performSelectorOnMainThread:。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top