-[UIColor isEqual:]
是比较颜色的内置方法。但是,您必须记住,虽然在特定设备上两种颜色在您看来可能相同,但它们在数学上可能不同,因此比较不平等。在您的眼睛看来不同的两种颜色(例如,RGB 中的“纯红色”与 CMYK 中的“纯红色”)在转换为中性颜色空间进行比较后,可能会比较相等。
色彩空间就是这样的 b_ _。:)
如果您告诉我们为什么要比较两种颜色,我们可能会为您提供比直接比较更好的解决方案。
回应您的评论:不要将您的用户界面点击测试基于单个甚至多个像素的颜色。它们的绝对值会根据设备颜色空间而变化,从而导致误报和误报。相反,您可以通过在界面中放置不可见的UIButton
对象来定义“热点”区域,或者更好的是,通过使用更灵活的东西CGPath
并测试触摸是否在其范围内。像下面这样的东西可能会起作用:
@interface HotSpottyView : UIView {
@private NSMutableDictionary *hotspots;
}
- (void)addHotspot: (CGPathRef)path withBlock: (void (^ handler)(void));
@end
@implementation HotSpottyView
- (id)initWithFrame: (CGRect)frameRect {
self = [super initWithFrame: frameRect];
if (self) {
self->hotspots = [[NSMutableDictionary alloc] init];
}
return self;
}
- (id)initWithCoder: (NSCoder *)aDecoder {
self = [super initWithFrame: frameRect];
if (self) {
self->hotspots = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc {
[self->hotspots release];
[super dealloc];
}
- (void)addHotspot: (CGPathRef)path withBlock: (void (^ handler)(void)) {
CGPathRef immutablePath = CGPathCreateCopy(path);
if (immutablePath) {
[self->hotspots setObject: handler forKey: (id)immutablePath];
CGPathRelease(immutablePath);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *t = [touches anyObject];
CGPoint loc = [t locationInView: self];
if (CGRectContainsPoint(self.bounds, loc)) {
for (id path in self->hotspots) {
if (CGPathContainsPoint((CGPathRef)path, NULL, loc, FALSE)) {
void (^ handler)(void) = [self->hotspots objectForKey: path];
handler();
}
}
}
}
@end