有没有可能从NSBezierPath
中移除由路径内的NSRect
区域定义的一块区域?
有没有可能从NSBezierPath
中移除由路径内的NSRect
区域定义的一块区域?
正如评论中所指出的那样,Caswell先生的回答实际上与提问者所问相反。此代码示例展示了如何从圆形(或任何贝塞尔曲线路径)中移除矩形(或任何其他贝塞尔曲线路径)。诀窍是要“反转”您想要移除的路径,然后将其附加到原始路径中:
NSBezierPath *circlePath = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(0, 0, 100, 100)];
NSBezierPath *rectPath = [NSBezierPath bezierPathWithRect:NSMakeRect(25, 25, 50, 50)];
rectPath = [rectPath bezierPathByReversingPath];
[circlePath appendBezierPath:rectPath];
注意:如果贝塞尔曲线相交,情况会变得有点棘手。那么你必须设置正确的“绕组规则”。
// Save the current clipping region
[NSGraphicsContext saveGraphicsState];
NSRect dontDrawThisRect = NSMakeRect(x, y, w, h);
// Either:
NSRectClip(dontDrawThisRect);
// Or (usually for more complex shapes):
//[[NSBezierPath bezierPathWithRect:dontDrawThisRect] addClip];
[myBezierPath fill]; // or stroke, or whatever you do
// Restore the clipping region for further drawing
[NSGraphicsContext restoreGraphicsState];
let path = NSBezierPath(ovalIn: NSRect(x: 0, y: 0, width: 100, height: 100))
let cutoutRect = NSBezierPath(rect: NSRect(x: 20, y: 20, width: 60, height: 60)).reversed
path.append(cutoutRect)
NSColor.red.setFill()
path.fill()