在循环中使用NSTimer

编程入门 行业动态 更新时间:2024-10-27 16:29:57
本文介绍了在循环中使用NSTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要显示一个从30到0的计时器,几次。 (30到0,从30开始,等等)然而,当我把它放在一个for循环,而不是等到定时器无效,开始下一个定时器,循环遍历和几个定时器创建。

如何构成一个等待计时器失效的循环?

- (IBAction)startCountdown:(id)sender {

NSNumber * rounds = [[NSNumber alloc] initWithInt:sliderRounds.value]; for(int i = rounds.intValue; i> 0; i--){ [self timer]; $ / code $ / $ p

解决方案

您可以使用scheduledTimerWithTimeInterval

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

在handleTimer内部创建下一个定时器

I need to show a timer counting down from 30 to 0, several times. (30 to 0, start over at 30, etc) However when I placed it in a for-loop, instead of waiting till the timer invalidates to begin the next timer, the loop iterates through and several timers are created.

How can I form a loop that waits until the timer has invalidated?

- (IBAction)startCountdown:(id)sender {

NSNumber *rounds = [[NSNumber alloc]initWithInt:sliderRounds.value]; for (int i = rounds.intValue; i > 0; i--) { [self timer]; }

解决方案

You can use the scheduledTimerWithTimeInterval

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

And inside the handleTimer you create the next timer

更多推荐

在循环中使用NSTimer

本文发布于:2023-10-26 01:24:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSTimer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!