我的應用程序聽到一些音頻水印,它工作正常。現在我想通過使用隱藏視圖來使這個應用變得非常漂亮,我在其中放置了一個標籤和一個按鈕。我在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
,但它不執行代碼來調用視圖。我不明白這是爲什麼,我希望你能幫我解決這個問題。
我無法在主線程中執行,因爲如果我在主線程中執行此代碼,它將阻止手機並且應用程序將崩潰... – lucgian841
@ lucgian841您需要在後臺執行'frequencyChangedWithValue:'方法線程,並在主線程上執行UI更新。你的UI更新不應該花太長的時間才能阻止。 – Gavin
@ lucgian841'frequencyChangedWithValue'線程正在運行什麼線程? – Gavin