2010-05-12 55 views

回答

1

Inside -drawRect:,您使用CGContextAddPath將其添加到上下文中,並使用CGContext(Draw|Fill|Stroke)Path來繪製它。即您子類NSView,並覆蓋

- (void)drawRect:(NSRect)needsDisplayInRect 
{ 
    CGContextRef cgcontext = [[NSGraphicsContext currentContext] graphicsPort]; 
    CGContextAddPath(cgcontext,path); // assumes you have CGPathRef*path; 
    CGContextStrokePath(cgcontext); 
} 

然後-drawRect:將在適當時調用。您可以通過調用[view displayIfNeeded]來強制更新視圖。

+1

CGContextAddPath實際上並沒有繪製路徑 - 您需要使用CGContext {Draw | Fill | Stroke}路徑。 – Chuck 2010-05-12 18:24:24

+0

Chuck是對的。這就是我最終做的。只是認爲會有一個更簡單的方法,但我猜不是。 – 2010-05-12 18:34:11

+0

你是對的。我會糾正它。 – Yuji 2010-05-12 18:48:38