2014-02-20 119 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