UIView的backgroundColor颜色循环
首先,我对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将起作用:
This will work:
视图需要时间来重绘,在第一个示例中,您设置了背景颜色,但视图不会重绘,直到您完成该方法。对于动画来说也是一样的,就像下面这样说:
我会使用 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:
I would use an NSTimer to loop through the colors.