Iphone 如何动态更改任何视图的背景色?

Iphone 如何动态更改任何视图的背景色?,iphone,objective-c,uiview,background-color,Iphone,Objective C,Uiview,Background Color,如何动态更改视图的背景色?我是否设置了计时器并指定视图的背景色属性以在循环中更改 如何实现此功能?听起来您只是想让视图每隔x个时间更改背景颜色。因此,对于所讨论的视图,您可以设置一个计时器,并在每次触发时更改背景颜色(您可以随机生成一些RGB值) 您可以使用+(NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)秒目标:(id)目标选择器:(SEL)选择器userInfo:(id)userInfo重复:(BOOL)重复将计时器设置为选择

如何动态更改视图的背景色?我是否设置了计时器并指定视图的背景色属性以在循环中更改


如何实现此功能?

听起来您只是想让视图每隔x个时间更改背景颜色。因此,对于所讨论的视图,您可以设置一个计时器,并在每次触发时更改背景颜色(您可以随机生成一些RGB值)

您可以使用+(NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)秒目标:(id)目标选择器:(SEL)选择器userInfo:(id)userInfo重复:(BOOL)重复将计时器设置为选择器,您可以在其中更改视图背景…请记住,UIKit不是线程s afe,因此如果计时器在另一个线程中运行,则应更改主线程上的视图背景

代码中的某个地方(viewDidAppear是一个好地方)

类中的方法:

- (void)changeBackground:(NSTimer *)timer {

    if (iWantToCancelTimer) {
        [timer invalidate];
    }

    self.view.backgroundColor = [UIColor whateverColor];
}
这将是一个突然的变化,所以您可能想要制作动画,但这是一个不同的问题

- (void)changeBackground:(NSTimer *)timer {

    if (iWantToCancelTimer) {
        [timer invalidate];
    }

    self.view.backgroundColor = [UIColor whateverColor];
}