如何自定义IKImageBrowserView以在每个项目上添加NSButton?

时间:2013-09-05 15:11:27

标签: cocoa nsview ikimagebrowserview ikimagebrowsercell

我想自定义IKImageBrowserView,以便我可以在IKImageBrowserView的单元格上添加NSButton或其他控件。我试图切割IKBrowserViewCell类,但我没有得到我添加NSbutton的方式和位置,因此它显示在IKBrowserViewItem上。 这是我迄今为止尝试过的代码。

#import "ImageBrowserView.h"
#import "ImageBrowserCell.h"

- (IKImageBrowserCell *) newCellForRepresentedItem:(id) cell
{
    return [[ImageBrowserCell alloc] init];
}

在细胞类

- (CALayer *) layerForType:(NSString*) type
{
    CGColorRef color;

    //retrieve some usefull rects
    NSRect frame = [self frame];
    NSRect imageFrame = [self imageFrame];
    NSRect relativeImageFrame = NSMakeRect(imageFrame.origin.x - frame.origin.x, imageFrame.origin.y - frame.origin.y, imageFrame.size.width, imageFrame.size.height);

//   place holder layer 
    if(type == IKImageBrowserCellPlaceHolderLayer){
        //create a place holder layer
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);

        CALayer *placeHolderLayer = [CALayer layer];
        placeHolderLayer.frame = *(CGRect*) &relativeImageFrame;

        float fillComponents[4] = {1.0, 1.0, 1.0, 0.3};
        float strokeComponents[4] = {1.0, 1.0, 1.0, 0.9};
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        //set a background color
        color = CGColorCreate(colorSpace, fillComponents);
        [placeHolderLayer setBackgroundColor:color];
        CFRelease(color);

        //set a stroke color
        color = CGColorCreate(colorSpace, strokeComponents);
        [placeHolderLayer setBorderColor:color];
        CFRelease(color);

        [placeHolderLayer setBorderWidth:2.0];
        [placeHolderLayer setCornerRadius:10];
        CFRelease(colorSpace);

        [layer addSublayer:placeHolderLayer];

        return layer;
    }
return nil;
}

此单元类具有返回IKImageBrowserCell的各种控件的框架的方法,并且不允许添加为子视图。 我没有得到我分配我的NSButton的地方,以便我可以在项目上显示。 请帮我。我已经浪费了大约一天的时间。 任何帮助都可以理解。 感谢

1 个答案:

答案 0 :(得分:1)

你朝着正确的方向前进。您需要检查类型并将按钮添加到Foreground图层。这是我现有项目的一些代码。

- (CALayer *)layerForType:(NSString *)type
{
    if (type == IKImageBrowserCellBackgroundLayer) {
        CALayer *layer = [CALayer layer];
        NSRect rect = [self frame];
        layer.frame = rect;
        _frameBg = rect;
        CGFloat cellSize = rect.size.height;

        SSLayerBackground *bgLayer = [SSLayerBackground layer];
        [layer addSublayer:bgLayer];
        [bgLayer setFrame:rect];
        bgLayer.position = NSMakePoint(rect.size.width/2.0, rect.size.height/2.0);
        [bgLayer setNeedsDisplay];

        /* Create background layer here */

        return layer;
    } else if (type == IKImageBrowserCellForegroundLayer) {
        CALayer *layer = [CALayer layer];
        NSRect rect = [self frame];
        layer.frame = rect;
        CGFloat cellSize = rect.size.width;


        /* Add your buttons to this layer */

        return layer;
    } else if (type == IKImageBrowserCellSelectionLayer) {
        CALayer *layer = [CALayer layer];
        NSRect rect = [self frame];
        layer.frame = rect;
        CGFloat cellSize = rect.size.height;

        /* Do selection layer stuff here */

        return layer;
    }
    //return nil;
    return [super layerForType:type];
}