How to customize IKImageBrowserView to add a NSBut

2019-07-11 17:47发布

问题:

I want to customize IKImageBrowserView so that i can add NSButton or other control on cell of IKImageBrowserView. I tried to cutomize IKBrowserViewCell class, but i did not get how and where i add NSbutton, So that it display on IKBrowserViewItem. Here is the code i had tried till now.

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

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

In cell class

- (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;
}

This cell class have method to return the frame of various control of IKImageBrowserCell and does not allow to add as subview. I did not get where i allocate my NSButton so that i can show then on item. Please help me. I already wasted about 1 day on this. Any help be appreciable. Thanks

回答1:

You're headed in the right direction. You need to check the type and add the buttons to the Foreground layer. Here's some code from an existing project of mine.

- (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];
}