Adding an NSControl to IKImageBrowserCell

吃可爱长大的小学妹 提交于 2020-01-23 01:44:11

问题


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



来源:https://stackoverflow.com/questions/4837543/adding-an-nscontrol-to-ikimagebrowsercell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!