0

私は以前に非常によく似た質問をしました(そして答えました)を知っていたので、その質問は解決できましたdirtyRect。したがって、どこに画像を描くべきかを知っていました。今、私はサブクラス化された同じ振る舞いを見ていますNSButtonCell

ここに画像の説明を入力してください

私のサブクラスではdrawImage:withFrame:inView、シャドウを追加するためにメソッドをオーバーライドしているだけです。

- (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView {   

    NSGraphicsContext *ctx = [NSGraphicsContext currentContext];
    [ctx saveGraphicsState];

    // Rounded Rect
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame cornerRadius:kDefaultCornerRadius];
    NSBezierPath *shadowPath = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 0.5, 0.5) cornerRadius:kDefaultCornerRadius]; // prevents the baground showing through the image corner

    // Shadow
    NSColor *shadowColor = [UAColor colorWithCalibratedWhite:0 alpha:0.8];
    [NSShadow setShadowWithColor:shadowColor
                  blurRadius:3.0
                      offset:NSMakeSize(0, -1)];
    [[NSColor colorWithCalibratedWhite:0.635 alpha:1.0] set];
    [shadowPath fill];
    [NSShadow clearShadow];

    // Background Gradient in case image is nil
    NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[UAColor grayColor] endingColor:[UAColor lightGrayColor]];
    [gradient drawInBezierPath:shadowPath angle:90.0];
    [gradient release];

    // Image
    if (image) {
        [path setClip];
        [image drawInRect:frame fromRect:CGRectZero operation:NSCompositeSourceAtop fraction:1.0];
    }

    [ctx restoreGraphicsState];

}

それはすべてかなり標準的なようですが、画像はまだ含まれているスクロールビューの境界の外側に描画されています。どうすればこれを回避できますか?

4

1 に答える 1

4

あなたが本当にそれを意味しない限り-setClip、あなたはを呼び出すべきではありません。一般的には、現在のクリッピング領域のセットにクリッピングパスを追加するが必要です。NSBezierPath‑addClip

NSScrollView独自のクリッピングパスを使用して機能し、画像を描画する前にそのパスを吹き飛ばします。

これも私を長い間つまずかせました。

于 2011-03-08T23:48:56.080 に答える