从NSBezierPath中剪切出一个矩形

Kri*_*oks 7 cocoa drawing objective-c nsbezierpath

是否有可能删除NSBezierPathNSRect路径中的某个区域定义的块?

Rob*_*rto 10

正如评论中所指出的,卡斯韦尔先生的回答实际上与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];
Run Code Online (Sandbox Code Playgroud)

注意:如果bezier路径相互交叉,事情会变得有点棘手.然后你必须设置适当的"缠绕规则".


Jos*_*ell 5

绝对.这是裁剪区域的作用:

// 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];
Run Code Online (Sandbox Code Playgroud)

  • NSRectClip不正确.它与当前的clippath相交.因此,在应用它之后,只绘制dontDrawThisRect中的内容,而不是从剪切区域中取出该区域.所以它与OP想要的完全相反. (4认同)