【问题标题】:Prevent NSButtonCell image from drawing outside of its containing NSScrollView防止 NSButtonCell 图像在其包含的 NSScrollView 之外绘制
【发布时间】:2011-03-08 22:59:49
【问题描述】:

我之前有asked(并已回答)一个非常相似的问题。这个问题能够得到解决,因为我知道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];

}

看起来都很标准,但图像仍在包含滚动视图边界之外绘制。我怎样才能避免这种情况?

【问题讨论】:

    标签: cocoa core-graphics nsimage nsscrollview nsbuttoncell


    【解决方案1】:

    你不应该在NSBezierPath 上调用-setClip,除非你真的是认真的。通常您需要‑addClip,它将您的剪切路径添加到当前剪切区域的集合中。

    NSScrollView 使用它自己的剪切路径工作,而您在绘制图像之前将这条路径吹走。

    这也让我绊倒了很长时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 2021-10-10
      相关资源
      最近更新 更多