【发布时间】:2010-06-09 09:29:58
【问题描述】:
我在 Cocoa 中使用 NSTimer 运行一个 mainLoop,如下所示:
mainLoopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/fps target:self selector:@selector(mainloop) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:mainLoopTimer forMode:NSEventTrackingRunLoopMode];
在程序启动时,我将 timeInterval 设置为 0.0,以便主循环尽可能快地运行。无论如何,我想提供一个函数来在运行时将帧速率(以及计时器的时间间隔)设置为特定值。不幸的是,据我所知,这意味着我必须重新初始化计时器,因为 Cocoa 不提供像“setTimerInterval”这样的功能 这是我尝试过的:
- (void)setFrameRate:(float)aFps
{
NSLog(@"setFrameRate");
[mainLoopTimer invalidate];
mainLoopTimer = nil;
mainLoopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/aFps target:self selector:@selector(mainloop) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:mainLoopTimer forMode:NSEventTrackingRunLoopMode];
}
但这会引发以下错误并停止主循环:
2010-06-09 11:14:15.868 myTarget[7313:a0f] setFrameRate 2010-06-09 11:14:15.868 myTarget[7313:a0f] * __NSAutoreleaseNoPool(): __NSCFDate 类的对象 0x40cd80 自动释放,没有适当的池 - 只是泄漏 2010-06-09 11:14:15.869 myTarget[7313:a0f] * __NSAutoreleaseNoPool(): NSCFTimer 类的对象 0x40e700 自动释放,没有适当的池 - 只是泄漏 0.614628
我还尝试使用“retain”关键字重新创建计时器,但这并没有改变任何东西。 关于如何在运行时动态更改 NSTimer 的时间间隔的任何想法?
谢谢!
【问题讨论】: