效果圖
- (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.0 target:self selector:@selector(testChangeViewBackColor)userInfo:nil repeats:YES];
// 啟動
[timer fire];
// label背景顏色改變
NSTimer *timer2 =[NSTimerscheduledTimerWithTimeInterval:1/50.0 target:self selector:@selector(testChangeLableBackColor)userInfo:nil repeats: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.0 green: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:red green:greenblue:blue alpha: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]];
}