NSTimer的射击比它应该更多

时间:2015-06-16 11:22:38

标签: ios objective-c selector nstimer

我确实理解这是我的问题,但不是NSTimer的问题,但如果有人能帮助我,我会非常感激。在我的项目中,我需要每0.5秒为2个对象调用此方法。问题是计时器在不同的时刻点火。它可能会立即发射3到5次(两个物体一起发射),然后它会在0.5秒后再次发射相同的声音。

-(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();

}

2 个答案:

答案 0 :(得分:2)

您无法跟踪现有的计时器,而是创建多个计时器,这就是您多次触发它们的原因。

使用实例变量,只有在当前无效时才创建计时器:

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];
}

答案 1 :(得分:2)

错误在这里完成预定计时器两次,这将导致在您不想要的不同时间调用相同的方法;

我更喜欢这里使用 performSelector:withObject:afterDelay:而不是 NSTimer

setState:method

中进行更改

请注意删除行:[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(blinkLamp) userInfo:nil repeats:NO];

(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];

    //.............
}

blinkLamp:方法

中进行更改

注意替换行:[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(blinkLamp) userInfo:nil repeats:NO];

使用:[self performSelector:@selector(blinkLamp) withObject:nil afterDelay:0.5];