gpt4 book ai didi

ios - 定时器后开始 Action

转载 作者:行者123 更新时间:2023-11-29 10:45:45 28 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我必须在延迟后执行操作。我会更好地解释它:我的应用程序可以识别一些音频:

  • 18100 赫兹
  • 18200 赫兹
  • 18300 赫兹
  • 18500 赫兹

为此,我修改了 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:你说的其他问题我都理解了,以后会解决的,暂时先解决timer的问题。 :)

最佳答案

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

另外,您的 if 语句有很多漏洞并且过于复杂。您已经检查过频率是否 > 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;
}
}

关于ios - 定时器后开始 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22452802/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com