2011-04-15 41 views

回答

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]; 
+0

NSRectClip是函數的正確名稱 – sergeyne 2013-04-12 12:19:44

+0

謝謝@sergeyne,已更正! – 2013-04-12 18:07:12

+4

NSRectClip不正確。它與當前的clippath相交。因此,在應用它之後,只會繪製dontDrawThisRect中的內容,而不是從裁剪區域中取出該區域。所以這與OP想要的完全相反。 – 2013-04-13 12:53:09

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]; 

注:事情變得有點棘手,如果貝塞爾路徑彼此交叉。然後你必須設置適當的「纏繞規則」。

相關問題