计时器后启动一个动作

时间:2014-03-17 11:11:52

标签: ios objective-c timer

在我的iOS应用中,我会在延迟后做一个动作。我会更好地解释它:我的应用程序可以识别一些音频频率:

  • 18100 Hz
  • 18200 Hz
  • 18300 Hz
  • 18500 Hz

为此,我修改了pitch detector。我想在设备无法识别我在我的应用中设置的频率之一时启动计时器。 我这样做了:

- (void)frequencyChangedWithValue:(float)newFrequency {
    frequencyRecived = newFrequency;
    watermarkReceived = YES;

    if (frequencyRecived > 18000) {
        if (frequencyRecived >= 18000 && frequencyRecived <= 18110 && !water1) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"1"];
            water2 = water3 = water4 = NO;
            water1 = YES;
        }
        if (frequencyRecived >= 18115 && frequencyRecived <= 18250 && !water2) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"2"];
            water1 = water3 = water4 = NO;
            water2 = YES;
        }
        if (frequencyRecived >= 18255 && frequencyRecived <= 18440 && !water3) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"3"];
            water1 = water2 = water4 = NO;
            water3 = YES;
        }
        if (frequencyRecived >= 18450 && !water4) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"4"];
            water1 = water2 = water3 = NO;
            water4 = YES;
        }
    } else {

        [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"Nessuna postazione"];
        water1 = water2 = water3 = water4 = NO;
    }
}

else部分,我要启动计时器,计算时间为10秒,在此之后我会写一个标签。我尝试使用以下解决方案:

[self performSelector:<#(SEL)#> withObject:<#(id)#> afterDelay:<#(NSTimeInterval)#> inModes:<#(NSArray *)#>]

,但它从不调用选择器方法,因为每次应用程序执行else中的代码时,它都会重新启动计时器。 你能建议我解决这个问题的解决方案吗? 感谢

更新

我按照答案,但它不起作用。我在这里发布了更新的代码:

@interface ViewController () {
    BOOL watermarkReceived;
    float frequencyRecived;
    CABasicAnimation *theAnimation;
    BOOL water1, water2, water3, water4;
}

@property(nonatomic,strong)NSTimer *timer;

@end

- (void)frequencyChangedWithValue:(float)newFrequency {
    frequencyRecived = newFrequency;
    watermarkReceived = YES;

    if (frequencyRecived > 18000) {

        [self.timer invalidate];
        self.timer = nil;

        if (frequencyRecived >= 18000 && frequencyRecived <= 18110 && !water1) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"1"];
            water2 = water3 = water4 = NO;
            water1 = YES;
        }
        if (frequencyRecived >= 18115 && frequencyRecived <= 18250 && !water2) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"2"];
            water1 = water3 = water4 = NO;
            water2 = YES;
        }
        if (frequencyRecived >= 18255 && frequencyRecived <= 18440 && !water3) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"3"];
            water1 = water2 = water4 = NO;
            water3 = YES;
        }
        if (frequencyRecived >= 18450 && !water4) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"4"];
            water1 = water2 = water3 = NO;
            water4 = YES;
        }
    } else {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(noPosition) userInfo:nil repeats:NO];
//        [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"Nessuna postazione"];
        water1 = water2 = water3 = water4 = NO;
    }
}

- (void)noPosition {
    [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"Nessuna postazione"];
}
PS:我理解你告诉我的其他问题,我将来会解决,现在我需要解决计时器问题。 :)

1 个答案:

答案 0 :(得分:2)

使用NSTimer。在else分支中,检查计时器是否已存在,如果不存在,则创建并计划它。在if的主分支中,使计时器失效并销毁。

此外,您的if语句有很多漏洞,而且过于复杂。您已经检查过频率是&gt; 18000所以你不需要再次检查它。您还使用if, else if来简化,因为每个都是一个不同的范围,如果检测到一个范围,则其他范围都不是。通过这种方式,您可以检查内部if中是否超出了上限范围。

另外,在后台设置文本看起来不对。 UI更新需要从主线程

进行

类似的东西:

- (void)frequencyChangedWithValue:(float)newFrequency {
    frequencyRecived = newFrequency;
    watermarkReceived = YES;

    if (frequencyRecived > 18000) {

        [self.timer invalidate];
        self.timer = nil;

        if (frequencyRecived <= 18110) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"1"];
            water2 = water3 = water4 = NO;
            water1 = YES;
        }
        else if (frequencyRecived <= 18250) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"2"];
            water1 = water3 = water4 = NO;
            water2 = YES;
        }
        else if (frequencyRecived <= 18440) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"3"];
            water1 = water2 = water4 = NO;
            water3 = YES;
        }
        else {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"4"];
            water1 = water2 = water3 = NO;
            water4 = YES;
        }
    } else {
        if (self.timer == nil) {
            self.timer = [NSTimer scheduledTimerWithTimeInterval:... target:self selector:... userInfo:nil repeats:NO];
        }

        water1 = water2 = water3 = water4 = NO;
    }
}