今天做项目的时候,遇到一个小问题,就是获取UIButton的背景颜色用来和已知颜色做对比,进行点击事件。去查了下文档,有个方法正好可以处理这个问题。
bool CGColorEqualToColor (CGColorRef color1, CGColorRef color2);
封装函数判断如下代码:
+ (BOOL) compareColor:(UIColor*)color1 anotherColor:(UIColor*)color2 { if (CGColorEqualToColor(color1.CGColor, color2.CGColor)) { return YES; } else { return NO; } }
而项目中的UIButton背景颜色判断:
if (CGColorEqualToColor(self.button_zhuzhuang.layer.backgroundColor, [UIColor redColor].CGColor)) { // } else { // }