iOS常用技术-七彩方块

 1 //
 2 //  UIColor+Radom.h
 3 //  七彩方块
 4 //
 5 //  Created by 大欢 on 16/1/18.
 6 //  Copyright © 2016年 bjsxt. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface UIColor (Radom)
12 
13 + (UIColor *)Random;
14 
15 @end

/***************************************************/

 1 //
 2 //  UIColor+Radom.m
 3 //  七彩方块
 4 //
 5 //  Created by 大欢 on 16/1/18.
 6 //  Copyright © 2016年 bjsxt. All rights reserved.
 7 //
 8 
 9 #import "UIColor+Radom.h"
10 
11 @implementation UIColor (Radom)
12 
13 + (UIColor *)Random {
14     
15     NSInteger r = arc4random()%255;
16     NSInteger g = arc4random()%255;
17     NSInteger b = arc4random()%255;
18     NSInteger a = arc4random()%10;
19 
20     return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a/10.0f];
21 }
22 
23 @end

/**************************************************************/

 1 //
 2 //  ViewController.m
 3 //  七彩方块
 4 //
 5 //  Created by 大欢 on 16/1/18.
 6 //  Copyright © 2016年 bjsxt. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "UIColor+Radom.h"
11 
12 @interface ViewController ()
13 
14 @end
15 
16 @implementation ViewController
17 
18 - (void)viewDidLoad {
19     [super viewDidLoad];
20    
21     [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addColorBlock) userInfo:nil repeats:YES];
22     
23 }
24 
25 - (void)addColorBlock {
26     
27     UIView * v = [[UIView alloc] init];
28     v.frame = CGRectMake([self randomVar1], [self randomVar2], [self randomSize], [self randomSize]);
29     v.backgroundColor = [UIColor Random];
30     [self.view addSubview:v];
31 }
32 
33 - (NSInteger)randomVar1 {
34     return arc4random()%400;
35 }
36 
37 - (NSInteger)randomVar2 {
38     return arc4random()%700;
39 }
40 
41 - (NSInteger)randomSize {
42     return arc4random()%100 + 50;
43 }
44 
45 - (void)didReceiveMemoryWarning {
46     [super didReceiveMemoryWarning];
47     // Dispose of any resources that can be recreated.
48 }
49 
50 @end


原文地址:https://www.cnblogs.com/MrWuYindi/p/5146494.html