【问题标题】:Use a transparent image on top of the standard background color of a View在 View 的标准背景颜色之上使用透明图像
【发布时间】:2013-03-28 16:54:22
【问题描述】:

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

【问题讨论】:

    标签: calayer nsview nsoutlineview


    【解决方案1】:

    这样的事情应该适合你的情况,

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

    我想说的是,

    你需要制作一个自定义的 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];
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-29
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多