本文介绍了在循环中使用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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!