Comparing colors in Objective-C

萝らか妹 提交于 2019-12-01 04:09:18
@implementation UIColor (compare)

- (BOOL) isEqualToColor:(UIColor *) otherColor
{
return CGColorEqualToColor(self.CGColor, otherColor.CGColor);
}

@end

Keep in mind that two colors that look the same may or may not return TRUE, since the components are kept as floats and they may differ by a value that's less than the display hardware can resolve.

Also keep in mind that if they're defined in different color spaces, this method will never return TRUE.

objects must be compared using the isEqual: method, not ==, which simply compares the pointer address

You're testing object pointers for equivalency, which is probably never going to return true. If you want to work with the actual color values then you'll need to get the underlying CGColor reference.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!