Adding an NSControl to IKImageBrowserCell

2020-07-24 04:00发布

问题:

I've built a custom IKImageBrowserCell which is displaying my images in an IKImageBrowser without any issues.

I'd like to try and override the built in IKImageBrowser delete image functionality. Currently 'out of the box' you can select an image, or multiple images and press BACKSPACE to delete.

I'd like to add an NSButton or similar to enable that same functionality on each image.

I've added the following code to show a delete icon on the IKImageBrowserCell when it is selected:

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

if(type == IKImageBrowserCellForegroundLayer){
    //no foreground layer on place holders
    if([self cellState] != IKImageStateReady)
        return nil;

    //create a foreground layer that will contain several childs layer
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);


    if([self isSelected]){

        //add a delete icon
        CALayer *deleteLayer = [CALayer layer];

        [deleteLayer setContents:(id)deleteImage()];
        deleteLayer.frame = CGRectMake(relativeImageFrame.size.width-14, (relativeImageFrame.origin.y+relativeImageFrame.size.height)-14, 28, 28);
        [layer addSublayer:deleteLayer];
    }
  }
}

This works great, but obviously just a static image. Is there any way I can try and get an event from hitting this delete icon, and then return the selected cell index to the IKImageBrowser in order to call it's removeItemsFromIndex: method ? Am stuck!

回答1:

myIKImageBrowserView.selectionIndexes() returns an NSIndexSet of the currently selected cell(s) - you can use that to call the removeItemsFromIndex method just before you delete the layer

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/IKImageBrowserView/index.html