NSScrollView中的图像绘制在其他视图之上 [英] Image inside NSScrollView drawing on top of other views

查看:435
本文介绍了NSScrollView中的图像绘制在其他视图之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义NSView,它位于 NSSplitView 中的 NSScrollView 自定义视图使用以下绘图代码:

I have a custom NSView that lives inside of a NSScrollView that is in a NSSplitView. The custom view uses the following drawing code:

- (void)drawRect:(NSRect)dirtyRect {
    NSGraphicsContext *ctx = [NSGraphicsContext currentContext];
    [ctx saveGraphicsState];

    // Rounded Rect
    NSRect rect = [self bounds];
    NSRect pathRect = NSMakeRect(rect.origin.x + 3, rect.origin.y + 6, rect.size.width - 6, rect.size.height - 6);
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:pathRect cornerRadius:kDefaultCornerRadius];

    // Shadow
    [NSShadow setShadowWithColor:[NSColor colorWithCalibratedWhite:0 alpha:0.66]
                      blurRadius:4.0
                          offset:NSMakeSize(0, -3)];     
    [[NSColor colorWithCalibratedWhite:0.196 alpha:1.0] set];
    [path fill];
    [NSShadow clearShadow];


    // Background Gradient
    NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[UAColor darkBlackColor] endingColor:[UAColor lightBlackColor]];
    [gradient drawInBezierPath:path angle:90.0];
    [gradient release];


    // Image
    [path setClip];
    NSRect imageRect = NSMakeRect(pathRect.origin.x, pathRect.origin.y, pathRect.size.height * kLargeImageRatio, pathRect.size.height);
    [self.image drawInRect:imageRect
                  fromRect:NSZeroRect
                 operation:NSCompositeSourceAtop
                  fraction:1.0];

    [ctx restoreGraphicsState];

    [super drawRect:dirtyRect];
}



我尝试了每种不同类型的 code>,但图像仍然绘制在 NSSplitView 的另一半的顶部:

... NSScrollView 。我认为这与绘制一切而不是 dirtyRect 只有,但我不知道如何我可以编辑图像绘制代码只绘制它的一部分位于 dirtyRect 中。 我如何阻止它绘制在顶部,或者只绘制这个NSImage的脏矩形?

…instead of drawing under the NSScrollView. I think this has to do with drawing everything instead of the dirtyRect only, but I don't know how I could edit the image drawing code to only draw the part of it that lies in the dirtyRect. How can I either prevent it from drawing on top, or only draw the dirty rect for this NSImage?

推荐答案

我终于得到了。我不知道它是否是最佳的,但我会发现什么时候做性能测试。我只是想通过使用 NSIntersectionRect 找出图像矩形和脏矩形的交集,然后找出 NSImage 的哪个子部分$ c>绘制 drawInRect:fromRect:operation:fraction:

I finally got it. I don't know if it is optimal yet, but I will find out when doing performance testing. I just had to figure out the intersection of the image rect and the dirty rect using NSIntersectionRect, then figuring out which subpart of the NSImage to draw for the drawInRect:fromRect:operation:fraction: call.

    NSRect imageRect = NSMakeRect(pathRect.origin.x, pathRect.origin.y, pathRect.size.height * kLargeImageRatio, pathRect.size.height);
    [self.image setSize:imageRect.size];

    NSRect intersectionRect = NSIntersectionRect(dirtyRect, imageRect);
    NSRect fromRect = NSMakeRect(intersectionRect.origin.x - imageRect.origin.x,
                                 intersectionRect.origin.y - imageRect.origin.y,
                                 intersectionRect.size.width,
                                 intersectionRect.size.height);
    [self.image drawInRect:intersectionRect
                  fromRect:fromRect
                 operation:NSCompositeSourceOver
                  fraction:1.0];

这篇关于NSScrollView中的图像绘制在其他视图之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆