我有以下绘图代码:
[[NSColor redColor] set];
NSRect fillRect = NSMakeRect(bounds.size.width - 20.0f, 0.0f, 20.0f, 20.0f);
NSBezierPath *bezier1 = [NSBezierPath bezierPathWithRoundedRect:fillRect xRadius:10.0f yRadius:10.0f];
[bezier1 fill];
NSRect fill2 = fillRect;
fill2.origin.x += 5;
fill2.origin.y += 5;
fill2.size.width -= 10.0f;
fill2.size.height -= 10.0f;
NSBezierPath *bezier2 = [NSBezierPath bezierPathWithRoundedRect:fill2 xRadius:5.0f yRadius:5.0f];
[[NSColor greenColor] set];
[bezier2 fill];这将导致以下结果:

如何确定内部的绿色圆圈是透明的?将绿色NSColor替换为透明颜色不起作用,逻辑;-)
有没有一种方法可以使NSBezierPath的实例相交,或者用另一种方法解决这个问题?
发布于 2012-06-18 03:27:00
我认为您正在寻找的是环的bezier路径,您可以通过创建一个NSBezierPath并设置缠绕规则来实现
[[NSColor redColor] set];
NSRect fillRect = NSMakeRect(bounds.size.width - 20.0f, 0.0f, 20.0f, 20.0f);
NSBezierPath *bezier1 = [NSBezierPath new];
[bezier1 setWindingRule:NSEvenOddWindingRule]; // set the winding rule for filling
[bezier1 appendBezierPathWithRoundedRect:fillRect xRadius:10.0f yRadius:10.0f];
NSRect innerRect = NSInsetRect(fillRect, 5, 5); // the bounding rect for the hole
[bezier1 appendBezierPathWithRoundedRect:innerRect xRadius:5.0f yRadius:5.0f];
[bezier1 fill];NSEvenOddWindingRule规则通过考虑从该点到整个路径边界之外的一条线来确定是否填充该点;如果该线穿过偶数条路径,则不填充该线,否则填充该线。因此,内圆中的任何点都不会被填充,而这两个点之间的点将是一个环。
https://stackoverflow.com/questions/11073041
复制相似问题