2014-02-20 47 views
1

我的应用程序听到一些音频水印,它工作正常。现在我想通过使用隐藏视图来使这个应用变得非常漂亮,我在其中放置了一个标签和一个按钮。我在Storyboard中绘制视图,然后将此视图放在视图控制器的顶部(正好位于x = 0和y = -200的位置)。现在我需要在手机听到水印时识别该视图(识别我使用的水印Pitch detector)。 我写下面的代码来识别该信号的频率:在iOS中显示隐藏视图

- (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,但它不执行代码来调用视图。我不明白这是为什么,我希望你能帮我解决这个问题。

回答

2

这个问题是因为你打电话performSelectorInBackground:,你在后台调用的那些方法正在改变UIKit元素。 UIKit元素的所有更改都需要在主线程上完成。因此需要在主线程上执行redLedsetTextInLabel:,而不是后台线程。

从主线程调用UIKit方法的确切行为在技术上未定义。从我所看到的情况来看,这样做有时会导致UI元素永远不会被更新。在其他一些情况下,它们确实会得到更新,但只有在很长时间的延迟之后或UI元素进行了其他更改后才会强制更新其状态。我认为你看到的情况是,它从来没有做你想做的事情,可能是因为你试图从另一个线程做动画,这很可能永远不会工作。

+0

我无法在主线程中执行,因为如果我在主线程中执行此代码,它将阻止手机并且应用程序将崩溃... – lucgian841

+0

@ lucgian841您需要在后台执行'frequencyChangedWithValue:'方法线程,并在主线程上执行UI更新。你的UI更新不应该花太长的时间才能阻止。 – Gavin

+0

@ lucgian841'frequencyChangedWithValue'线程正在运行什么线程? – Gavin