本文介绍了UIView backgroundColor颜色循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是新的这个Xcode / Objective-C的东西,所以去容易在我! :)
我做了一个测试应用程序,通过按一些按钮,背景变化。我有一个红色,蓝色,绿色,白色,黑色和复活按钮。

First of all, I am new to this Xcode/Objective-C thing, so go easy on me! :)I made a test app that, by pressing some buttons, the background changes. I have a red, blue, green, white, black and revive button.

我通过按下所有的颜色按钮使应用程序改变backgrnd的颜色。但是,我想让应用程序循环通过颜色,说100倍非常快,当按下Revive按钮。由于某种原因,它不工作。

I made the app change the color of the backgrnd by pressing all the color buttons. However, I want to make the app cycle through the colors, say 100 times very fast when pressing the Revive button. For some reason, it doesnt work.

以下是无效的代码:

使用下面的代码,最后一个颜色。

Using the code below, only changes to the last color.

- (IBAction)Revive:(id)sender {

    for (int y=0; y < 100; y++) {
    view1.backgroundColor = [UIColor redColor];
    view1.backgroundColor = [UIColor greenColor];
    view1.backgroundColor = [UIColor blueColor];
    view1.backgroundColor = [UIColor whiteColor];
    view1.backgroundColor = [UIColor blackColor];
                                }
}

使用下面的代码,从白色变为黑色

Using the code below, whitout the loop, the app fades from white to black

- (IBAction)Revive:(id)sender {

    [UIView animateWithDuration:0.2 animations:^{
    view1.backgroundColor = [UIColor redColor];
    view1.backgroundColor = [UIColor greenColor];
    view1.backgroundColor = [UIColor blueColor];
    view1.backgroundColor = [UIColor whiteColor];
    view1.backgroundColor = [UIColor blackColor];
    }];

 [UIView commitAnimations];

}

任何人都知道为什么会发生这种情况, ?

Anyone knows why this is happening and a solution to my problem?

推荐答案

这将有效:

- (void) doBackgroundColorAnimation {
    static NSInteger i = 0;
    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor whiteColor], [UIColor blackColor], nil];

    if(i >= [colors count]) {
        i = 0;
    }

    [UIView animateWithDuration:2.0f animations:^{
        self.view.backgroundColor = [colors objectAtIndex:i];
    } completion:^(BOOL finished) {
        ++i;
        [self doBackgroundColorAnimation];
    }];

}

这篇关于UIView backgroundColor颜色循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:46