ios - 在 iOS 中显示隐藏 View

标签 ios iphone objective-c uikit

我的应用程序正在监听某些音频水印,并且运行正常。现在我想通过使用一个隐藏 View 来使这个应用程序更漂亮,我在其中放置了一个标签和一个按钮。我在 Storyboard 中绘制了一个 View ,然后将此 View 放在 View Controller 的顶部(恰好位于 x = 0 和 y = -200 的位置)。现在我需要在手机听到水印时显示此 View (为了识别我使用的水印 Pitch detector )。 我编写了以下代码来识别信号的频率:

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

    NSLog(@"%f", frequencyRecived);

    if (frequencyRecived > 18000) {
        if (frequencyRecived >= 18000 && frequencyRecived <= 18140 && !water1) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"1"];
            water2 = water3 = water4 = NO;
            water1 = YES;
        }
        if (frequencyRecived >= 18150 && frequencyRecived <= 18290 && !water2) {
            [self performSelectorInBackground:@selector(setTextInLabel:) withObject:@"2"];
            water1 = water3 = water4 = NO;
            water2 = YES;
        }
        if (frequencyRecived >= 18300 && 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(redLed) withObject:nil];
    }
}

选择如下:

- (void)redLed {
    [self.imageLed setImage:[UIImage imageNamed:@"image_led_red.png"]];
    self.labelPosition.font=[UIFont fontWithName:@"DBLCDTempBlack" size:20.0];
}

- (void)setTextInLabel:(NSString*)position {
    self.labelPosition.font=[UIFont fontWithName:@"DBLCDTempBlack" size:20.0];
    self.labelPosition.text = position;
    self.labelPositionHiddenView.text = position;
    NSString *textForToast = [NSString stringWithFormat:@"Postazione %@", position];
    [self.view makeToast:textForToast duration:3.0 position:@"bottom"];
    [self.imageLed setImage:[UIImage imageNamed:@"image_led_green.png"]];
}

选择器 setTextInLabel 在设备识别频率时被调用,所以我更新了选择器:

- (void)setTextInLabel:(NSString*)position {
    self.labelPosition.font=[UIFont fontWithName:@"DBLCDTempBlack" size:20.0];
    self.labelPosition.text = position;
    self.labelPositionHiddenView.text = position;
    // Instructions to recall my hide view
    [UIView transitionWithView:self.view
                      duration:0.5
                       options:UIViewAnimationOptionAllowAnimatedContent
                    animations:^{
                        [self.viewHidden setFrame:CGRectOffset(self.viewHidden.frame, 0, 340)];
                    }
                    completion:nil];
    //-----------------------------
    NSString *textForToast = [NSString stringWithFormat:@"Postazione %@", position];
    [self.view makeToast:textForToast duration:3.0 position:@"bottom"];
    [self.imageLed setImage:[UIImage imageNamed:@"image_led_green.png"]];
}

当我尝试运行应用程序并且设备听到水印时它识别它,它调用选择器 setTextInLabel,但它不执行代码来调用 View 。我不明白这是为什么,我希望你能帮助我解决这个问题。

最佳答案

问题是因为您正在调用 performSelectorInBackground:,而您在后台调用的那些方法正在对 UIKit 元素进行更改。对 UIKit 元素的所有更改都需要在主线程上完成。所以redLedsetTextInLabel:都需要在主线程执行,而不是后台线程。

从主线程调用 UIKit 方法的确切行为在技术上是未定义的。据我所知,这样做有时会导致 UI 元素永远不会更新。在其他一些情况下,它们确实会更新,但只是在长时间延迟之后或在 UI 元素对其进行了一些其他更改以强制其刷新其状态之后。我认为您遇到的情况是它永远不会执行您希望它执行的操作,这可能是因为您尝试从另一个线程执行动画,而这很可能永远不会奏效。

关于ios - 在 iOS 中显示隐藏 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21913527/

相关文章:

ios - AVAudioPlayer 音量条件未正确返回

iOS - 水平 UIStackView 中控件的顶部和底部边距

iphone - 有没有办法从新的模态视图访问启动模态视图的 View Controller ?

objective-c - iOS编程自动释放池

iphone - 如何确定哪个 UIAlertView 调用了委托(delegate)。

iphone - YouTube WebView - 视频 ID

ios - viewControllerAfterViewController : does not get called with UIPageViewController

iphone - 在 iPhone 中使用 html 查找当前位置

iphone - NSString 的 isEqualToString 的奇怪结果

ios - 尝试在 iOS 8 中以横向模式而不是纵向模式显示键盘