IOS timer NSTimer random acquisition color acquisition and setting RGB view background color

Source: Internet
Author: User



-(Void) viewDidLoad

{

[Super viewDidLoad];

// Label initialization color

[_ Label setBackgroundColor: [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 100];

// View background color change

// 1/20. 0. The error may occur once if the execution is performed 20 times in one second.

NSTimer * timer = [NSTimerscheduledTimerWithTimeInterval: 1/20. 0 target: self selector: @ selector (testChangeViewBackColor) userInfo: nilrepeats: YES];

// Start

[Timer fire];

// The label background color changes.

NSTimer * timer2 = [NSTimerscheduledTimerWithTimeInterval: 1/50. 0 target: self selector: @ selector (testChangeLableBackColor) userInfo: nilrepeats: YES];

[Timer2 fire];

}


-(Void) testChangeLableBackColor

{

// Obtain the background color

UIColor * color = [self-> _ labelbackgroundColor];

Const CGFloat * components = CGColorGetComponents (color. CGColor );

// Obtain the RGB color

CGFloat red = components [0];

CGFloat green = components [1];

CGFloat blue = components [2];

// This is what I debugged. Originally, I wanted to make it <255 discover this effect. If anyone knows what it means, share it with me.

// I guess this is to make 1. When it is not black 2. I don't know

If (red! = 1)

{

// Set the label. Each time rgb + 1 is passed in, the label cannot contain an integer.

// Alpha: 0.5 sets translucent

[Self-> _ labelsetBackgroundColor: [UIColorcolorWithRed: red + 1.0/255.0 green: green + 1.0/255.0 blue: blue + 1.0/255.0 alpha: 0.5];

}

Else

{

// A time factor must be set before the random RGB color is specified. Otherwise, each time is the same.

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

}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.