在视图的标准背景颜色之上使用透明图像

时间:2013-03-28 16:54:23

标签: calayer nsview nsoutlineview

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

1 个答案:

答案 0 :(得分: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];
}