是否可以删除路径中NSBezierPath
区域定义的NSRect
块?
答案 0 :(得分:9)
正如评论中所指出的,卡斯韦尔先生的答案实际上与OP提出的要求相反。此代码示例演示如何从圆形中删除矩形(或从任何其他贝塞尔曲线路径中删除任何贝塞尔曲线路径)。诀窍是“反转”您要删除的路径,然后将其附加到原始路径:
NSBezierPath *circlePath = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(0, 0, 100, 100)];
NSBezierPath *rectPath = [NSBezierPath bezierPathWithRect:NSMakeRect(25, 25, 50, 50)];
rectPath = [rectPath bezierPathByReversingPath];
[circlePath appendBezierPath:rectPath];
注意:如果bezier路径相互交叉,事情会变得有点棘手。然后你必须设置适当的“缠绕规则”。
答案 1 :(得分:5)
绝对。这就是clipping regions所做的:
// 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];