Matt Gemmell的NSBezierPath + StrokeExtensions在clip rect之外绘制

时间:2014-07-25 02:02:41

标签: cocoa drawing drawrect clipping nsbezierpath

我使用Matt Gemmell的NSBezierPath + StrokeExtensions类别在NSRect上绘制内部笔划。以下是整个类别的代码:

- (void)strokeInside
{
    /* Stroke within path using no additional clipping rectangle. */
    [self strokeInsideWithinRect:NSZeroRect];
}

- (void)strokeInsideWithinRect:(NSRect)clipRect
{
    NSGraphicsContext *thisContext = [NSGraphicsContext currentContext];
    float lineWidth = [self lineWidth];

    /* Save the current graphics context. */
    [thisContext saveGraphicsState];

    /* Double the stroke width, since -stroke centers strokes on paths. */
    [self setLineWidth:(lineWidth * 2.0)];

    /* Clip drawing to this path; draw nothing outwith the path. */
    [self setClip];

    /* Further clip drawing to clipRect, usually the view's frame. */
    if (clipRect.size.width > 0.0 && clipRect.size.height > 0.0) {
        [NSBezierPath clipRect:clipRect];
    }

    /* Stroke the path. */
    [self stroke];

    /* Restore the previous graphics context. */
    [thisContext restoreGraphicsState];
    [self setLineWidth:lineWidth];
}

这是我的drawRect:方法:

- (void)drawRect:(NSRect)dirtyRect {
    NSRect myRect = NSMakeRect(500, 0, 400, 100);
    [[NSColor colorWithCalibratedRed:0 green:0 blue:1.0 alpha:0.5] set];
    NSRectFillUsingOperation(myRect, NSCompositeSourceOver);

    [[NSColor blueColor] set];
    [[NSBezierPath bezierPathWithRect:myRect] strokeInside];
}

然而,当我向上滚动时会发生这种情况:

enter image description here

如您所见,内部笔划绘制到工具栏上。为什么会这样?如何修复strokeInside方法?请注意,常规stroke方法不会发生这种情况。

1 个答案:

答案 0 :(得分:1)

好的,我明白了。而不是这一行:

[self setClip];

使用此:

[self addClip];

工作正常且有意义。