0

我有一个使用setBackroundColor:方法设置自定义背景颜色的 NSOutlineView。之后,我不想在该背景颜色之上绘制透明图像。我尝试使用图层,但它不起作用。有谁知道该怎么做?谢谢

4

1 回答 1

0

这样的事情应该适用于你的情况,

只需从我的应用程序中粘贴相关代码

我想说的是,

你需要制作一个自定义的 NSOutliveView 并覆盖它的 drawRect,

如果它不适用于您的情况,请使用自定义 NSView 并绘制透明的 NSOutlineView

- (void)drawRect:(NSRect)rect
{
     switch (_backgroundStyle) {
     case ImageColorBackgroundStyle:
     {  
         [self drawColor];
         [self drawCustomImage:rect];
         break;
     }
     case ColorBackgroundStyle:
     [self drawColor];
     break;
     case PatternBackgroundStyle:
     [self drawPattern];
     break;
     case ImageBackgroundStyle:
     [self drawImage];
     break;
     case GradientBackgroundStyle:
     [self drawGradient];
     break;
     case NoBackgroundStyle:
             if([self hasBorder])
                 [self drawBorder:rect];
             return;
             [[NSColor clearColor] set];
             NSRectFill(rect);
             break;
     default:
     [super drawRect:rect];
     break;
     }  
    if([self hasBorder])
        [self drawBorder:rect];


}

-(void)drawCustomImage:(NSRect)rect{
    NSRect boundsRect = [self bounds];

    NSRect imageRect;
    NSSize imageSize = [pBGroundImage size];
    int x=0;int y=0;
    switch (bgImagePosition) {
        case img_bg_lowerright:
            if(bViewClipped) return;
            x = boundsRect.size.width - imageSize.width;
                x -= (BORDER_WIDTH*2);

            break;
        default:
            if(!bViewClipped)
                return [self drawImage];
            break;
    }

    if(!bViewClipped)
    imageRect = NSMakeRect(x,y,imageSize.width,imageSize.height);
    else {
        imageRect = NSMakeRect(x,y,rect.size.width,rect.size.height);
    }

    if(imageRect.origin.x < 0 ) imageRect.origin.x = 0;

    if(!bViewClipped){
        [pBGroundImage drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    }
    else {
        CGFloat x_Res = 0.0f;
        CGFloat y_Res = 0.0f;


        x_Res = orignalSize.width/imageSize.width;
        y_Res = orignalSize.height/imageSize.height;

        CGFloat scaledHeight = orignalSize.height - rect.size.height;
        CGFloat orignal_y = scaledHeight/y_Res;
        CGFloat orignal_width = imageSize.height - orignal_y;
        NSRect srcRect;

        /* This is wrt Orignal Image */
        srcRect.origin.x = 0; 
        srcRect.origin.y = orignal_y;
        srcRect.size.width = imageSize.width;
        srcRect.size.height = orignal_width;

        [pBGroundImage drawInRect:rect 
                         fromRect:srcRect  
                         operation:NSCompositeSourceOver fraction:1.0];

    }
}

- (void)drawImage
{
    // Load the image.
    //NSString *imageName = [self GetBackGroundImage];

    //NSImage *anImage = [[NSImage alloc ]initByReferencingFile:imageName];

    // Find the point at which to draw it.
    NSPoint backgroundCenter;
    backgroundCenter.x = [self bounds].size.width / 2;
    backgroundCenter.y = [self bounds].size.height / 2;

    NSPoint drawPoint = backgroundCenter;
    drawPoint.x -= [pBGroundImage size].width / 2;
    drawPoint.y -= [pBGroundImage size].height / 2;

    [pBGroundImage drawInRect:[self bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    //[anImage release];
}
于 2013-04-02T15:06:48.083 回答