iphone 随机颜色生成

iphone 随机颜色生成

使用类别来为UIColor添加方法:

 #import <UIKit/UIKit.h>

 
@interface UIColor(Random)
+(UIColor *)randomColor;
@end
 

 

实现文件:

 

@implementation UIColor(Random)
+(UIColor *)randomColor
{
    static BOOL seeded = NO;
    if (!seeded) {
        seeded = YES;
        srandom(time(NULL));
    }
    CGFloat red =  (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
@end
posted on 2013-01-04 17:18  easonoutlook  阅读(674)  评论(0编辑  收藏  举报