- (void)viewDidLoad {
[super viewDidLoad];
// Label初始化颜色
[_label setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:100]];
// view背景颜色改变
// 1/20.0 让1秒内执行20次可能会误差1次
NSTimer *timer =[NSTimerscheduledTimerWithTimeInterval:1/20.0target:self selector:@selector(testChangeViewBackColor)userInfo:nilrepeats:YES];
// 启动
[timer fire];
// label背景颜色改变
NSTimer *timer2 =[NSTimerscheduledTimerWithTimeInterval:1/50.0target:self selector:@selector(testChangeLableBackColor)userInfo:nilrepeats:YES];
[timer2 fire];
}
-(void) testChangeLableBackColor
{
// 获取背景色
UIColor *color = [self->_labelbackgroundColor];
const CGFloat *components = CGColorGetComponents(color.CGColor);
// 获取RGB颜色
CGFloat red = components[0];
CGFloat green = components[1];
CGFloat blue =components[2];
// 这个是我调试出来的本来我是想让它<255的 发现这样出不来效果如果有谁知道是什么意思求分享啊
// 我猜这个是让 1.不是黑色的时候 2.以到达峰值不知道那个
if(red!=1)
{
// 设置标签每次rgb+1 注意这里传入的时候不能带有整数
// alpha:0.5 设置半透明
[self->_labelsetBackgroundColor:[UIColorcolorWithRed:red+1.0/255.0green:green+1.0/255.0 blue:blue+1.0/255.0alpha:0.5]];
}
else
{
// 随机RGB颜色前提要先有时间因子不然每次都是一样
time(NULL);
CGFloat red = (CGFloat)random() / (CGFloat)RAND_MAX;
CGFloat green = (CGFloat)random() / (CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)random() / (CGFloat)RAND_MAX;
[ self->_labelsetBackgroundColor:[UIColorcolorWithRed:redgreen:greenblue:bluealpha:0.5]];
}
}
-(void) testChangeViewBackColor
{
static BOOL seeded =NO;
if(!seeded)
{
seeded =YES;
time(NULL);
}
CGFloat red = (CGFloat)random() / (CGFloat)RAND_MAX;
CGFloat green = (CGFloat)random() / (CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)random() / (CGFloat)RAND_MAX;
[ _myview setBackgroundColor:[UIColor colorWithRed:red green:green blue:blue alpha:1]];
}