UIView的backgroundColor颜色循环

发布于 2024-11-13 11:21:18 字数 1084 浏览 7 评论 0原文

首先,我对 Xcode/Objective-C 很陌生,所以对我要轻松点! :) 我制作了一个测试应用程序,通过按一些按钮,背景会发生变化。我有一个红色、蓝色、绿色、白色、黑色和恢复按钮。

我通过按所有颜色按钮让应用程序更改背景颜色。但是,我想让应用程序循环显示颜色,例如按下“Revive”按钮时速度非常快 100 倍。由于某种原因,它不起作用。

以下是不起作用的代码:

使用下面的代码,仅更改为最后一种颜色。

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

使用下面的代码,没有循环,应用程序从白色渐变为黑色

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

}

有人知道为什么会发生这种情况以及我的问题的解决方案吗?

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.

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.

The following is the code that isn't working:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言月老 2024-11-20 11:21:18

这将起作用:

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

}

This will work:

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

}
自找没趣 2024-11-20 11:21:18

视图需要时间来重绘,在第一个示例中,您设置了背景颜色,但视图不会重绘,直到您完成该方法。对于动画来说也是一样的,就像下面这样说:

int x = 0;
x = 5;
x = 6;
// why is x 6 ? :(

我会使用 NSTimer 来循环颜色。

The view needs time to redraw, in the first example you have you set the backgroundcolor, but the view doesn't redraw untill you are done with that method. With the animation its the same thing, its like saying the following:

int x = 0;
x = 5;
x = 6;
// why is x 6 ? :(

I would use an NSTimer to loop through the colors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文