ios - 基于定时器的带有变量的 if 语句

标签 ios objective-c if-statement avaudiorecorder

我有一个定时器设置,可以触发和读取 iPhone 麦克风输入电平。我有一些 if 语句,说明如果传入量是“this”,则将 View 背景颜色更改为“this”。

一切正常,但仪表自然会依次为 0、1、2、3、4、5,然后以 5、4、3、2、1、0 的相反顺序返回。但是我不希望这种情况发生。当音量试图回落时,我想一直跳回“0”。

所以我所做的是创建两个变量。一个称为“previousValue”,另一个称为“currentValue”,以跟踪它们在我所处的连续级别中的位置 - 然后是最后一个 if 语句,表示如果 previousValue > currentValue(下降)然后跳回“0”。但是,它只是行不通。也许我在这里做错了什么?

如果能提供一点帮助,我们将不胜感激!谢谢你!这是代码,因此您可以更好地了解我想要实现的目标。

我在实现下面声明了 INT 值 int previousValue; int 当前值;

然后在viewDidLoad中设置一个起点 先前值 = 0; 当前值 = 0;

if (meterResults > 0.0) {
    NSLog(@"0");
    self.view.backgroundColor = [UIColor redColor];
    previousValue = currentValue;
    currentValue = 0;
}

if (meterResults > 0.1){
    NSLog(@"1");
    self.view.backgroundColor = [UIColor yellowColor];
    previousValue = currentValue;
    currentValue = 1;
}

if (meterResults > 0.2){
    NSLog(@"2");
    self.view.backgroundColor = [UIColor blueColor];
    previousValue = currentValue;
    currentValue = 2;
}

if (meterResults > 0.3){
    NSLog(@"3");
    self.view.backgroundColor = [UIColor greenColor];
    previousValue = currentValue;
    currentValue = 3;
}

if (meterResults > 0.4){
    NSLog(@"4");
    self.view.backgroundColor = [UIColor purpleColor];
    previousValue = currentValue;
    currentValue = 4;
}

if (previousValue > currentValue) {

    NSLog(@"Hello, don't go in reverse order go straight to Red");

    self.view.backgroundColor = [UIColor redColor];

}

最佳答案

Aroth 的回答是对的,但是你听了之后还是有问题。由于您正在对模拟信号进行采样,因此连续测量将大致连续。如果您将当前值更改为零,则读取的下一个值将在您的代码中显示为再次增加,但事实并非如此。

不要谎报当前值以使用户界面按照您的意愿运行。相反,对您关心的事物进行显式建模,即瞬时一阶导数。那会是这样的:

currentValue = (int)floor(meterResults * 10.0);
NSInteger currentFirstDerivative = currentValue - previousValue;
// since the timer repeats at a fixed interval, this difference is like a 1st derivative

if (currentFirstDerivative < 0) {
    // it's going down, make the view red
} else {
    switch (currentValue) {
    case 1:
        // change colors here
        break;
    case 2:
    // and so on
}

// now, for next time
previousValue = currentValue;

关于ios - 基于定时器的带有变量的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14619549/

相关文章:

ios - 替换出现的字符串模式

ios加载不会导致动画卡顿

ios - isDataAvailable 导致 warnParseOperationOnMainThread() Parse.com

php - !empty() 对于值 0.0000 结果为 true

ios - 在 Swift 中向 UILocalNotification 添加文本字段

objective-c - TWIN PRIMES BETWEEN 2 VALUES 错误的结果

objective-c - Swift 使用 hash_hmac 和 ripemd160 散列字符串

iphone - 工具栏不会显示在键盘上方

java - 如果 HTTP 请求之一失败,JMeter 会发送电子邮件

java - 为什么我的 "While"循环一直持续下去?