cocoa – 更改重复计时器的NSTimer间隔

cocoa – 更改重复计时器的NSTimer间隔,第1张

概述我正在使用NSTimer设置在 Cocoa中运行mainLoop,如下所示: mainLoopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/fps target:self selector:@selector(mainloop) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoo 我正在使用NSTimer设置在 Cocoa中运行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,以便mainloop尽可能快地运行.无论如何,我想提供一个函数来在运行时将帧速率(以及计时器的时间间隔)设置为特定值.不幸的是,据我所知,这意味着我必须重新初始化计时器,因为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(): Object 0x40cd80 of class __NSCFDate autoreleased with no pool in place – just leaking
2010-06-09 11:14:15.869 myTarget[7313:a0f] * __NSautoreleasenopool(): Object 0x40e700 of class NSCFTimer autoreleased with no pool in place – just leaking
0.614628

我还尝试使用“retain”关键字重新创建计时器,但这并没有改变任何东西.
有关如何在运行时动态更改NSTimer间隔的任何想法?

谢谢!

解决方法 我知道,这个线程太神奇了!
但我也在寻找功能!

事实证明,NSTimes没有提供这个功能,我决定建立一个自己的功能.
这不是最好的解决方案,但就我而言,它是唯一可行的解​​决方案.

我定义了变量“BOol keepOn;”在我的标题中.

-(IBAction)doSomething:(ID)sender{    // Any action to throw every interval.}-(IBAction)switchautoLoad:(ID)sender{    if([autoLoad state] == NSOffState)    {        NSLog(@"auto-reload on!");        self performSelector:@selector(fireTimer) withObject:nil afterDelay:0];        keepOn = YES;        [autoLoad setState:NSOnState];    }    else    {        NSLog(@"auto-reload off!");        keepOn = NO;        [autoLoad setState:NSOffState];    }}-(voID)fireTimer{    [self doSomething:nil];    float theTime = 20; // This is the time interval.    if(keepOn)    {        [self performSelector:@selector(fireTimer) withObject:nil afterDelay:theTime];    }}

(非常)bis问题是,“计时器”正在等待整个延迟,然后才能获得新值.
你看,它没有快速响应:))
你不能立即停止它(必须等待延迟完成).

如果你停止它,它会再投掷一个动作.

实际上它适用于我的,适用于小应用程序和工具.

总结

以上是内存溢出为你收集整理的cocoa – 更改重复计时器的NSTimer间隔全部内容,希望文章能够帮你解决cocoa – 更改重复计时器的NSTimer间隔所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1009262.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存