创建UIColor上的类别,名称为Random
头文件UIColor+Random.h代码如下:
#import <UIKit/UIKit.h>@interface UIColor (Random)/* 生成随机颜色 */+(UIColor *)randomColor;@end
源文件UIColor+Random.m代码如下:
#import "UIColor+Random.h"@implementation UIColor (Random)+(UIColor *)randomColor{ static BOOL seed = NO; if (!seed) { seed = YES; srandom(time(NULL)); } CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX; CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX; CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX; return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];//alpha为1.0,颜色完全不透明}@end
ps:srandom()的作用可以参考:http://blog.csdn.net/m_changgong/article/details/7797451