0

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

ただし、上にスクロールすると次のようになります。

ここに画像の説明を入力

ご覧のとおり、内側のストロークがツールバーに描画されます。なぜこれが起こるのですか?strokeInsideメソッドを修正するにはどうすればよいですか?strokeこれは、通常の方法では発生しないことに注意してください。

4

1 に答える 1

1

わかりました。この行の代わりに:

[self setClip];

これを使って:

[self addClip];

うまく機能し、理にかなっています。

于 2014-07-25T03:36:08.293 に答える