【发布时间】:2013-07-13 08:07:28
【问题描述】:
是否可以删除由路径中的NSRect 区域定义的NSBezierPath 块?
【问题讨论】:
标签: objective-c cocoa drawing nsbezierpath
是否可以删除由路径中的NSRect 区域定义的NSBezierPath 块?
【问题讨论】:
标签: objective-c cocoa drawing nsbezierpath
正如 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];
注意:如果贝塞尔路径相互交叉,事情会变得有点棘手。然后你必须设置正确的“缠绕规则”。
【讨论】:
当然。这就是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];
【讨论】: