【问题标题】:Creating a NSView from a CGPath从 CGPath 创建 NSView
【发布时间】:2010-05-12 17:12:22
【问题描述】:

我有一个 CGPath,我想将它绘制一次到 NSView。看起来比较简单,但我在 AppKit(非 iphone)中没有找到方法。

【问题讨论】:

    标签: macos nsview appkit cgpath


    【解决方案1】:

    -drawRect: 内部,您使用CGContextAddPath 将其添加到上下文中,并使用CGContext(Draw|Fill|Stroke)Path 绘制它。 IE。你继承 NSView,并覆盖

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

    然后将在适当的时候调用-drawRect:。您可以通过调用[view displayIfNeeded] 来强制更新视图。

    【讨论】:

    • CGContextAddPath 实际上并没有绘制路径——您需要为此使用 CGContext{Draw|Fill|Stroke}Path。
    • 查克是对的。这就是我最终做的。只是以为会有更简单的方法,但我想没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多