答案来自 user1118321,但我发布这个答案是为了有内联图片而不是内联回复。首先,我为维恩图选择了一组更好的颜色:
这些颜色相互重叠和隐藏。解决方案是使用 kCGBlendModeScreen:
然而,这增加了原始颜色。解决方案是将背景设置为黑色:
对于那些好奇或懒惰的代码,这里有一些代码。在 touchesBegan/Ended/Moved 事件中,我正在创建 SMVennObjects,然后在 drawRect 中绘制它们。SMVennObject 仅包含两个 CGPoints 和一个 UIColor(使用静态 int 按顺序分配)。
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect{
for(NSUInteger index = 0; index < self.vennObjects.count; index++){
NSLog(@"drawing index %d", index);
SMVennObject* vo = [self.vennObjects objectAtIndex:index];
[self drawVennObject:vo context:UIGraphicsGetCurrentContext()];
}
}
-(void)drawVennObject:(SMVennObject*)vo context:(CGContextRef)cgContext{
if((vo.pointBegin.x == 0 && vo.pointBegin.y == 0) ||
(vo.pointEnd.x == 0 && vo.pointEnd.y == 0)){
return;
}
CGContextBeginPath(cgContext);
CGContextSetLineWidth(cgContext, 2.0f);
// Convert UIColor to raw values
CGFloat red = 0.0;
CGFloat green = 0.0;
CGFloat blue = 0.0;
CGFloat alpha = 0.0;
[vo.color getRed:&red green:&green blue:&blue alpha:&alpha];
alpha = 1.0;
CGFloat color[4] = {red, green, blue, alpha};
CGContextSetBlendMode(cgContext, kCGBlendModeScreen);
CGRect r = CGRectMake(MIN(vo.pointBegin.x, vo.pointEnd.x),
MIN(vo.pointBegin.y, vo.pointEnd.y),
fabs(vo.pointBegin.x - vo.pointEnd.x),
fabs(vo.pointBegin.y - vo.pointEnd.y));
// Draw ellipse
CGContextSetFillColor(cgContext, color);
CGContextFillEllipseInRect(cgContext, r);
// Draw outline of ellipse
CGContextSetStrokeColor(cgContext, color);
CGContextStrokeEllipseInRect(cgContext, r);
}