如何忽略 Objective-C 中的 NSTimer TimeInterval?
How to ignore NSTimer TimeInterval in Objective-C?
我在使用每 1 秒触发一次的 NSTimer 时遇到以下情况。在下面的代码中,当我单击重新启动时,我想立即转到第一个条件,但这直到 1 秒事件之后才会发生。任何想法如何在没有一秒钟延迟的情况下做到这一点?
int condition =0; //can only be 0, 1, or 2
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timerEvent:) userInfo:nil repeats:YES];
[timer fire];
- (void)timerFired:(NSTimer *)timer {
seconds++;
if(condition ==0)
[self drawFoo];
if(condition ==1)
[self drawBoo];
if(condition == 2)
[self drawYoo];
if(seconds >= duration){
condition++;
seconds =0;
}
}
- (IBAction)reStart:(id)sender {
condition = 0;
}
尝试在您的 viewDidLoad 中调用它,因为它会在视图加载时执行您想要的操作:)
您需要立即启动计时器,然后重新触发:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerEvent:) userInfo:nil repeats:NO];
[timer fire];
- (void)timerFired:(NSTimer *)timer {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:@selector(timerEvent:) userInfo:nil repeats:NO];
[timer fire];
seconds++;
/...
如果我理解正确的话,似乎有两种明显的方法可以解决您的问题。第一种是通过调用分支中的相同代码直接立即触发 if (condition == 0)
语句的分支,即
- (IBAction)reStart:(id)sender {
condition = 0;
[self drawFoo];
}
或者,您可以设置稍微不同的代码格式,并在 timerFired
方法中触发一个单独的方法,然后在 reStart:
方法中调用此方法。实际上,这与第一个选项相同,但是它更干净且更可重用。
- (void)timerFired:(NSTimer *)timer {
seconds++;
[self someCommonMethod:timer];
condition++;
}
- (void)someCommonMethod:(id)param {
if (condition == 0)
[self drawFoo];
if (condition == 1)
[self drawBoo];
if (condition == 2)
[self drawYoo];
}
- (IBAction)reStart:(id)sender {
condition = 0;
[self someCommonMethod:someParamForConditions];
}
我在使用每 1 秒触发一次的 NSTimer 时遇到以下情况。在下面的代码中,当我单击重新启动时,我想立即转到第一个条件,但这直到 1 秒事件之后才会发生。任何想法如何在没有一秒钟延迟的情况下做到这一点?
int condition =0; //can only be 0, 1, or 2
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timerEvent:) userInfo:nil repeats:YES];
[timer fire];
- (void)timerFired:(NSTimer *)timer {
seconds++;
if(condition ==0)
[self drawFoo];
if(condition ==1)
[self drawBoo];
if(condition == 2)
[self drawYoo];
if(seconds >= duration){
condition++;
seconds =0;
}
}
- (IBAction)reStart:(id)sender {
condition = 0;
}
尝试在您的 viewDidLoad 中调用它,因为它会在视图加载时执行您想要的操作:)
您需要立即启动计时器,然后重新触发:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerEvent:) userInfo:nil repeats:NO];
[timer fire];
- (void)timerFired:(NSTimer *)timer {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:@selector(timerEvent:) userInfo:nil repeats:NO];
[timer fire];
seconds++;
/...
如果我理解正确的话,似乎有两种明显的方法可以解决您的问题。第一种是通过调用分支中的相同代码直接立即触发 if (condition == 0)
语句的分支,即
- (IBAction)reStart:(id)sender {
condition = 0;
[self drawFoo];
}
或者,您可以设置稍微不同的代码格式,并在 timerFired
方法中触发一个单独的方法,然后在 reStart:
方法中调用此方法。实际上,这与第一个选项相同,但是它更干净且更可重用。
- (void)timerFired:(NSTimer *)timer {
seconds++;
[self someCommonMethod:timer];
condition++;
}
- (void)someCommonMethod:(id)param {
if (condition == 0)
[self drawFoo];
if (condition == 1)
[self drawBoo];
if (condition == 2)
[self drawYoo];
}
- (IBAction)reStart:(id)sender {
condition = 0;
[self someCommonMethod:someParamForConditions];
}