码迷,mamicode.com
首页 > 移动开发 > 详细

iOS常用技术-七彩方块

时间:2016-01-20 22:14:59      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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
技术分享

iOS常用技术-七彩方块

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!