如何处理多个NSTimer?

发布于 2024-11-30 02:57:37 字数 232 浏览 2 评论 0原文

我有一个函数,可以说 onTimer

-(void)onTimer {
       * Some Operation *
}

我想以这种方式调用这个方法...

在 10 秒内,它应该每 0.2 秒调用一次...然后在另外 10 秒内,调用该方法的持续时间应该增加... ..通过这样做,操作会从快速模式变得缓慢......并且最终会停止。

请指导。

I have a function lets say onTimer

-(void)onTimer {
       * Some Operation *
}

I want to call this method like in this way...

For 10 seconds it should call on every 0.2 seconds.... then in another 10seconds, duration of calling this method should be increased..... by doing this it will appear operations getting slow from fast mode... and it will stop at the end.

Please guide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里寻她 2024-12-07 02:57:37

我认为用 2 个计时器就可以很容易地完成这个任务。在 .h 文件中,声明 2 个计时器:

float intervalYouWant;
NSTimer * timer1;
NSTimer * timer2;

在 .m 文件中,

- (void)viewDidLoad; {
  intervalYouWant = 0.2;
  timer1 = [NSTimer scheduledTimerWithTimeInterval:intervalYouWant target:self selector:@selector(methodForTimer1) userInfo:nil repeats:YES];
  timer2 = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(changeTimer1) userInfo:nil repeats:YES];
}

- (void)changeTimer1; {
  [timer1 invalidate];
  timer1 = nil;
  intervalYouWant += amountYouWantToAdd;
  timer1 = [NSTimer scheduledTimerWithTimeInterval:intervalYouWant target:self selector:@selector(methodForTimer1) userInfo:nil repeats:YES];
}

应每 10 秒取消第一个计时器,并以新的时间间隔重新启动它。不要忘记在 dealloc 方法中使计时器无效。希望有帮助!

I think this is fairly easy to accomplish with 2 timers. In the .h file, declare 2 timers:

float intervalYouWant;
NSTimer * timer1;
NSTimer * timer2;

In the .m file,

- (void)viewDidLoad; {
  intervalYouWant = 0.2;
  timer1 = [NSTimer scheduledTimerWithTimeInterval:intervalYouWant target:self selector:@selector(methodForTimer1) userInfo:nil repeats:YES];
  timer2 = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(changeTimer1) userInfo:nil repeats:YES];
}

- (void)changeTimer1; {
  [timer1 invalidate];
  timer1 = nil;
  intervalYouWant += amountYouWantToAdd;
  timer1 = [NSTimer scheduledTimerWithTimeInterval:intervalYouWant target:self selector:@selector(methodForTimer1) userInfo:nil repeats:YES];
}

That should cancel the first timer every 10 seconds, and restart it with a new time interval. Don't forget to invalidate the timers in the dealloc method. Hope that helps!

远山浅 2024-12-07 02:57:37

以非重复模式启动计时器

float interval   = 0.2; //global variable

[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timerSelector:) userInfo:nil repeats:NO];

..........
..........

-(void) timerSelector:(NSTimer *)timer{
  static float timeConsumed = 0.0;
  //do your task which you want to do here
  ............
  ............

 // in the end
   if(timeConsumed > 10.0){
     interval    = 0.5;   //increase the interval so it decrease the speed..
   }else if(timeConsumed > 20.0){
     interval    = 1.0;
   }... go on like this until you stop it..

   timeConsumed   += interval;
   [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timerSelector:) userInfo:nil repeats:NO];
}

从内存中编写..因此可能存在语法错误..纠正自己..希望这会有所帮助..

Start the timer in non repeat mode

float interval   = 0.2; //global variable

[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timerSelector:) userInfo:nil repeats:NO];

..........
..........

-(void) timerSelector:(NSTimer *)timer{
  static float timeConsumed = 0.0;
  //do your task which you want to do here
  ............
  ............

 // in the end
   if(timeConsumed > 10.0){
     interval    = 0.5;   //increase the interval so it decrease the speed..
   }else if(timeConsumed > 20.0){
     interval    = 1.0;
   }... go on like this until you stop it..

   timeConsumed   += interval;
   [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timerSelector:) userInfo:nil repeats:NO];
}

Written from memory..So syntax errors possible..Correct yourself..hope this helps..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文