【问题标题】:Cut a rectangle out of an NSBezierPath从 NSBezierPath 中剪出一个矩形
【发布时间】:2013-07-13 08:07:28
【问题描述】:

是否可以删除由路径中的NSRect 区域定义的NSBezierPath 块?

【问题讨论】:

    标签: objective-c cocoa drawing nsbezierpath


    【解决方案1】:

    正如 cmets 所述,Caswell 先生的回答实际上与 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];
    

    注意:如果贝塞尔路径相互交叉,事情会变得有点棘手。然后你必须设置正确的“缠绕规则”。

    【讨论】:

      【解决方案2】:

      当然。这就是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];
      

      【讨论】:

      • NSRectClip 不正确。它与当前剪辑路径相交。因此,在应用它之后,仅绘制 dontDrawThisRect 中的内容,而不是从剪切区域中取出该区域。所以这与 OP 想要的完全相反。
      • 我可以用它来删除我程序中绘制的圆圈吗?我正在尝试在我的程序中为一个圆圈设置动画。圆圈沿着一条路径移动,所以我正在做的是在 for 循环的每次迭代中创建一个新圆圈。但是如何删除旧圆圈?有人可以帮帮我吗?
      猜你喜欢
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      相关资源
      最近更新 更多