I’d like for events running in a thread dedicated to audio to change the UI. Simply calling view.backgroundColor doesn’t seem to have any effect.
Here are two methods in my viewController. The first is triggered by touches. The second is called from the audio code. The first works. The second. Any idea why?
// this changes the color
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[touchInterpreter touchesMoved:touches withEvent:event];
self.view.backgroundColor = [UIColor colorWithWhite: 0.17 + 2 * [patch getTouchInfo]->touchSpeed alpha:1];
};
// this is called from the audio thread and has no effect
-(void)bang: (float)intensity{
self.view.backgroundColor = [UIColor colorWithWhite: intensity alpha:1];
}
Any idea why? Am I just doing something stupid, or is there a trick to changing UI elements from outside the run loop?
Touching the UI from anything other than the main thread is not allowed, and will lead to strange behavior or crashes. On iOS 4.0 or later you should use something like
Or the NSOperationQueue variant
On iOS 3.2 or earlier, you can use
[self performSelectorOnMainThread:@selector(setViewBackgroundColor:) withObject:[UIColor colorWithWhite:intensity alpha:1] waitUntilDone:NO]and then just defineNote that calling
[self.view performSelectorOnMainThread:@selector(setBackgroundColor:) withObject:[UIColor colorWithWhite:intensity alpha:1] waitUntilDone:NO]is not safe because theviewproperty of UIViewController is not thread-safe.