有没有办法立即更改窗口的背景颜色?
我需要一个闪烁的背景,即红/绿色在一秒钟的间隔内闪烁。正如我所看到的,背景颜色不会立即更改,而只有在离开函数时才会更改。
是否有任何方法强制系统立即更改并重新绘制窗口的背景?
我需要一个闪烁的背景,即红/绿色在一秒钟的间隔内闪烁。正如我所看到的,背景颜色不会立即更改,而只有在离开函数时才会更改。
是否有任何方法强制系统立即更改并重新绘制窗口的背景?
- (void)viewDidLoad
{
[super viewDidLoad];
flag = YES;
NSTimer *mTimer = [NSTimer scheduledTimerWithTimeInterval:.5
target:self
selector:@selector(changeColor)
userInfo:nil
repeats:YES];
}
- (void)changeColor
{
if (flag == YES)
{
self.view.backgroundColor = [UIColor redColor];
flag = NO;
return;
}
self.view.backgroundColor = [UIColor blueColor];
flag = YES;
}
Naveen已经做了一个不错的开始,但你可以通过为颜色变化添加动画效果来展示更高级的技巧。
- (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;
}
self.changeBgColourTimer = ...
开头。 - Abizern@property (nonatomic, strong) NSTimer *changeBgColourTimer;
- guenis