【问题标题】:Concave NSBezierPath凹 NSBezierPath
【发布时间】:2012-06-18 03:26:59
【问题描述】:

我有如下绘图代码:

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

【问题讨论】:

  • 您的问题还不清楚。您能否以另一种方式重述您正在尝试做的事情?

标签: cocoa appkit nsbezierpath


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    相关资源
    最近更新 更多