Ios NSTimer发射的信号超过了它应该发射的信号
我明白这是我的问题,但不是NSTimer的问题,但如果有人能帮助我,我将非常感激。在我的项目中,我需要为2个对象每隔0.5秒调用此方法。问题是计时器在不同的时刻触发。它可能会立即发射3或5次(两个物体同时发射),然后在0.5秒后再次发射Ios NSTimer发射的信号超过了它应该发射的信号,ios,objective-c,selector,nstimer,Ios,Objective C,Selector,Nstimer,我明白这是我的问题,但不是NSTimer的问题,但如果有人能帮助我,我将非常感激。在我的项目中,我需要为2个对象每隔0.5秒调用此方法。问题是计时器在不同的时刻触发。它可能会立即发射3或5次(两个物体同时发射),然后在0.5秒后再次发射 -(void) blinkLamp{ switch (currentState) { case blinkingGreen: NSLog(@"blink green lamp"); self.greenLamp = !se
-(void) blinkLamp{
switch (currentState) {
case blinkingGreen:
NSLog(@"blink green lamp");
self.greenLamp = !self.greenLamp;
self.colorState[0] = [NSNumber numberWithBool:greenLamp];
self.rndValuesChanged = rand();
break;
case blinkingYellow:
NSLog(@"blink yellow lamp");
self.yellowLamp = !self.yellowLamp;
self.colorState[1] = [NSNumber numberWithBool:yellowLamp];
self.rndValuesChanged = rand();
break;
default:
break;
}
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(blinkLamp) userInfo:nil repeats:NO];
}
此方法在SetState方法中调用一次
-(void) setState:(State)newState{
currentState = newState;
switch (newState) {
case green:
self.greenLamp = YES;
self.yellowLamp = NO;
self.redLamp = NO;
break;
case yellow:
self.greenLamp = NO;
self.yellowLamp = YES;
self.redLamp = NO;
break;
case red:
self.greenLamp = NO;
self.yellowLamp = NO;
self.redLamp = YES;
break;
case redYellow:
self.greenLamp = NO;
self.yellowLamp = YES;
self.redLamp = YES;
break;
case off:
self.greenLamp = NO;
self.yellowLamp = NO;
self.redLamp = NO;
break;
case blinkingGreen:
self.greenLamp = YES;
self.yellowLamp = NO;
self.redLamp = NO;
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(blinkLamp) userInfo:nil repeats:NO];
break;
case blinkingYellow:{
self.greenLamp = NO;
self.yellowLamp = YES;
self.redLamp = NO;
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(blinkLamp) userInfo:nil repeats:NO];
//[self blinkLamp];
break;}
default:
NSLog(@"This mode is not allowed for VehicleTL");
break;}
NSLog(@"G - %d Y - %d R - %d", self.greenLamp, self.yellowLamp, self.redLamp);
self.colorState[0] = [NSNumber numberWithBool:greenLamp];
self.colorState[1] = [NSNumber numberWithBool:yellowLamp];
self.colorState[2] = [NSNumber numberWithBool:redLamp];
self.rndValuesChanged = rand();
}您无法跟踪现有计时器,而是创建了多个计时器,这就是为什么要让它们多次触发 使用实例变量,并且仅在计时器当前无效时创建计时器:
case blinkingGreen:
self.greenLamp = YES;
self.yellowLamp = NO;
self.redLamp = NO;
[self createBlinkingTimer];
break;
case blinkingYellow:{
self.greenLamp = NO;
self.yellowLamp = YES;
self.redLamp = NO;
[self createBlinkingTimer];
//[self blinkLamp];
...
- (void)createBlinkingTimer
{
if (!self.blinkingTimer.isValid)
self.blinkingTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(blinkLamp) userInfo:nil repeats:NO];
}
您无法跟踪现有计时器,而是创建了多个计时器,这就是为什么要让它们多次触发的原因 使用实例变量,并且仅在计时器当前无效时创建计时器:
case blinkingGreen:
self.greenLamp = YES;
self.yellowLamp = NO;
self.redLamp = NO;
[self createBlinkingTimer];
break;
case blinkingYellow:{
self.greenLamp = NO;
self.yellowLamp = YES;
self.redLamp = NO;
[self createBlinkingTimer];
//[self blinkLamp];
...
- (void)createBlinkingTimer
{
if (!self.blinkingTimer.isValid)
self.blinkingTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(blinkLamp) userInfo:nil repeats:NO];
}
此处执行的错误两次定时,这将导致在不同的时间调用相同的方法,这是您不想要的 我更愿意在这里使用性能选择器:withObject:afterDelay:而不是NSTimer 在设置状态:方法中进行更改 注意删除行:
[NSTimer scheduledTimerWithTimeInterval:0.5目标:自选择器:@selector(Blinklight)用户信息:无重复:否]代码>
(void) setState:(State)newState
{
//.............
//.............
switch (newState) {
//...............
}
//The method should be called once only
if(self.greenLamp || self.yellowLamp)
[self performSelector:@selector(blinkLamp) withObject:nil afterDelay:0.5];
//.............
}
在闪烁灯:方法中进行更改
注:替换行:[NSTimer scheduledTimerWithTimeInterval:0.5目标:自选择器:@selector(Blinklight)用户信息:无重复:否]代码>
(void) setState:(State)newState
{
//.............
//.............
switch (newState) {
//...............
}
//The method should be called once only
if(self.greenLamp || self.yellowLamp)
[self performSelector:@selector(blinkLamp) withObject:nil afterDelay:0.5];
//.............
}
带有:[自执行选择器:@选择器(闪烁灯),对象:零延时:0.5]
此处执行的错误两次定时,这将导致在不同的时间调用相同的方法,这是您不想要的
我更愿意在这里使用性能选择器:withObject:afterDelay:而不是NSTimer
在设置状态:方法中进行更改
注意删除行:[NSTimer scheduledTimerWithTimeInterval:0.5目标:自选择器:@selector(Blinklight)用户信息:无重复:否]代码>
(void) setState:(State)newState
{
//.............
//.............
switch (newState) {
//...............
}
//The method should be called once only
if(self.greenLamp || self.yellowLamp)
[self performSelector:@selector(blinkLamp) withObject:nil afterDelay:0.5];
//.............
}
在闪烁灯:方法中进行更改
注:替换行:[NSTimer scheduledTimerWithTimeInterval:0.5目标:自选择器:@selector(Blinklight)用户信息:无重复:否]代码>
(void) setState:(State)newState
{
//.............
//.............
switch (newState) {
//...............
}
//The method should be called once only
if(self.greenLamp || self.yellowLamp)
[self performSelector:@selector(blinkLamp) withObject:nil afterDelay:0.5];
//.............
}
带有:[自执行选择器:@选择器(闪烁灯),对象:零延时:0.5]
在创建新的nstimer时,您需要关闭早期的nstimer…添加到@FahimParkar的注释中,您需要使已完成的计时器无效。在启动一个新的nstimer之前,类似于[myTimer invalidate]
的内容。创建新的nstimer时,您需要关闭以前的nstimer…添加到@FahimParkar的注释中,您将希望使计时器无效,因为您已经使用了它。类似于[myTimer invalidate]
的东西,然后再启动一个新的。谢谢!实际上,我决定创建一个变量来包含当前计时器,每次需要设置新计时器时,我都会清理它。谢谢!实际上,我决定创建一个变量来包含当前计时器,每次需要设置新计时器时,我都会清理它。