我的应用听到一些音频水印,它工作正常。现在我想通过使用隐藏视图使我的应用程序非常漂亮,我在其中放置标签和按钮。我在Storyboard中绘制了一个视图,然后我将此视图放在视图控制器的顶部(恰好位于x = 0和y = -200的位置)。现在我需要在手机听到水印时显示此视图(识别我使用的水印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
,但它不会执行代码来调用视图。我不明白为什么,我希望你能帮我解决这个问题。
答案 0 :(得分:2)
问题是因为您正在调用performSelectorInBackground:
,而您在后台调用的那些方法正在对UIKit元素进行更改。 UIKit元素的所有更改都需要在主线程上完成。因此,redLed
和setTextInLabel:
都需要在主线程上执行,而不是后台线程。
从主线程调用UIKit方法的确切行为在技术上是未定义的。从我所看到的,这样做有时会导致UI元素永远不会更新。在其他一些情况下,它们会更新,但只有在长时间延迟之后或者在UI元素对其进行了一些其他更改后才会更新其状态。我认为你看到的情况是它从来没有做过你想要它做的事情,可能是因为你试图从另一个线程做动画,这很可能永远不会奏效。