我的应用程序听到一些音频水印,它工作正常。现在我想通过使用隐藏视图来使这个应用变得非常漂亮,我在其中放置了一个标签和一个按钮。我在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