//
// UIColor+Radom.h
// 七彩方块
//
// Created by 大欢 on 16/1/18.
// Copyright © 2016年 bjsxt. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIColor (Radom)
+ (UIColor *)Random;
@end
/***************************************************/
//
// UIColor+Radom.m
// 七彩方块
//
// Created by 大欢 on 16/1/18.
// Copyright © 2016年 bjsxt. All rights reserved.
//
#import "UIColor+Radom.h"
@implementation UIColor (Radom)
+ (UIColor *)Random {
NSInteger r = arc4random()%255;
NSInteger g = arc4random()%255;
NSInteger b = arc4random()%255;
NSInteger a = arc4random()%10;
return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a/10.0f];
}
@end
/**************************************************************/
//
// ViewController.m
// 七彩方块
//
// Created by 大欢 on 16/1/18.
// Copyright © 2016年 bjsxt. All rights reserved.
//
#import "ViewController.h"
#import "UIColor+Radom.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addColorBlock) userInfo:nil repeats:YES];
}
- (void)addColorBlock {
UIView * v = [[UIView alloc] init];
v.frame = CGRectMake([self randomVar1], [self randomVar2], [self randomSize], [self randomSize]);
v.backgroundColor = [UIColor Random];
[self.view addSubview:v];
}
- (NSInteger)randomVar1 {
return arc4random()%400;
}
- (NSInteger)randomVar2 {
return arc4random()%700;
}
- (NSInteger)randomSize {
return arc4random()%100 + 50;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end