如何强制iOS立即更改背景颜色?

时间:2011-06-09 08:56:56

标签: iphone xcode ios animation

有没有办法立即改变窗口的背景颜色?

我需要一个闪烁的背景,即红色/绿色以一秒的间隔闪烁。正如我所看到的,背景颜色不会立即改变,只有在剩下功能时才会改变。

有没有办法强制系统更改它并立即重绘窗口的背景?

2 个答案:

答案 0 :(得分:6)

- (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;

}

答案 1 :(得分:4)

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;
}