1

我有以下绘图代码:

[[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 的实例相交或用另一种方法解决这个问题?

4

1 回答 1

3

我认为您正在寻找的是环的贝塞尔路径,您可以通过创建单个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规则通过考虑从该点到整个路径边界之外的线来确定是否填充特定点;如果该线穿过偶数条路径,则未填充,否则已填充。因此,内圈中的任何点都不会被填充,而两者之间的点将是 - 形成一个环。

于 2012-06-17T19:26:59.783 回答