NSTimer 发射超过它应该的

NSTimer firing more than it should

我知道这是我的问题,而不是 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();

}

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

使用实例变量,仅在定时器当前无效时才创建定时器:

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

这里犯了个错误定时定时器两次这将导致在不同的时间调用相同的方法,这是你不希望的;

我更愿意在这里使用 performSelector:withObject:afterDelay: 而不是 NSTimer

setState: 方法中进行更改

注意删除行:[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]