有什么方法可以立即更改窗口的背景颜色?
我需要闪烁的背景,即红色/绿色每隔一秒闪烁一次。如我所见,背景颜色不会立即改变,而仅在功能保留时才会改变。
是否有任何方法可以强制系统进行更改并立即重绘窗口背景?
最佳答案
Naveen开创了良好的开端,但您可以通过设置颜色变化的动画来展示更多的 class 。
- (void)viewDidLoad {
[super viewDidLoad];
// Set up the initial background colour
self.view.backgroundColor = [UIColor redColor];
// Set up a repeating timer.
// This is a property,
self.changeBgColourTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeColour) userInfo:nil repeats:YES];
}
- (void) changeColour {
// Don't just change the colour - give it a little animation.
[UIView animateWithDuration:0.25 animations:^{
// No need to set a flag, just test the current colour.
if ([self.view.backgroundColor isEqual:[UIColor redColor]]) {
self.view.backgroundColor = [UIColor greenColor];
} else {
self.view.backgroundColor = [UIColor redColor];
}
}];
// Now we're done with the timer.
[self.changeBgColourTimer invalidate];
self.changeBgColourTimer = nil;
}