【发布时间】:2016-11-18 15:59:38
【问题描述】:
【问题讨论】:
-
你可以使用 NSTimer 吗?
标签: ios objective-c uiview colors background-color
【问题讨论】:
标签: ios objective-c uiview colors background-color
要改变你刚刚做的背景颜色:
self.view.backgroundColor=yourColor;
要连续更改,您可以使用计时器。如果您有一个数组颜色作为背景,请将颜色存储在一个数组中。
NSArray *colorArray=@[colo1,color2....colorn];
您还可以生成random 颜色并将其传递给视图背景。在这种情况下,您可以这样做(在这种情况下,您不必将颜色存储在数组中)
self.view.backgroundColor=metodThatReturnsRandomColor.
或者,声明每 1.0 秒调用 changeColor 方法的计时器。
@property (nonatomic, retain) NSTimer * timerIvar;
self.timerIvar = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];
方法是:
-(void)changeColor{
if(x<colorArray.count){
self.view.backgroundColor=colorArray[x];
}else{
x=0;
}
x++;
}
同样的逻辑也适用于图片/gradient colors。
并记住 viewWillDisappear
- (void)viewWillDisappear:(BOOL)animated
{
...
[self.timerIvar invalidate];
self.timerIvar = nil;
}
【讨论】: