IOS: setting the background color of a UIView from outside a startup loop

I would like events triggered in the audio thread to change the interface. Just calling view.backgroundColor does not seem to have any effect.

There are two methods in my viewController. The first is caused by touches. The second is called from the audio code. First work. 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 user interface elements due to a startup loop?

+3
source share
1 answer

-, , . iOS 4.0 -

- (void)bang:(float)intensity {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.view.backgroundColor = [UIColor colorWithWhite:intensity alpha:1];
    });
}

NSOperationQueue

- (void)bang:(float)intensity {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        self.view.backgroundColor = [UIColor colorWithWhite:intensity alpha:1];
    }];
}

iOS 3.2 [self performSelectorOnMainThread:@selector(setViewBackgroundColor:) withObject:[UIColor colorWithWhite:intensity alpha:1] waitUntilDone:NO],

- (void)setViewBackgroundColor:(UIColor *)color {
    self.view.backgroundColor = color;
}

, [self.view performSelectorOnMainThread:@selector(setBackgroundColor:) withObject:[UIColor colorWithWhite:intensity alpha:1] waitUntilDone:NO] , view UIViewController .

+6

Source: https://habr.com/ru/post/1773174/


All Articles