[英]How can I force iOS to change background color immediately?
有什么方法可以立即更改 window 的背景顏色?
我需要一個閃爍的背景,即每隔一秒閃爍一次紅色/綠色。 如我所見,背景顏色不會立即改變,但只有在 function 離開時才會改變。
有什么辦法可以強制系統改變它並立即重繪窗口的背景?
- (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 提供了一個良好的開端,但您可以通過設置顏色變化的動畫來顯示更多 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.