IOS 定時器 NSTimer 隨機擷取顏色 擷取與設定 RGB view背景色

來源:互聯網
上載者:User



- (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]];

}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.