我已經構建了一個自定義的IKImageBrowserCell,它在IKImageBrowser中顯示我的圖像沒有任何問題。添加NSControl到IKImageBrowserCell
我想嘗試重寫內置的IKImageBrowser刪除圖像功能。目前「開箱即用」,您可以選擇一個圖像或多個圖像,然後按BACKSPACE刪除。
我想添加一個NSButton或類似的,以便在每個圖像上啓用相同的功能。
我已經添加以下代碼顯示在IKImageBrowserCell刪除圖標選中時:
- (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];
}
}
}
這個偉大的工程,但顯然只是一個靜態圖像。有什麼辦法可以嘗試從點擊這個刪除圖標中獲取一個事件,然後將選定的單元格索引返回給IKImageBrowser以調用它的removeItemsFromIndex:方法?我卡住了!
參見HTTP:// stackoverflow.com/questions/8839230/adding-nsbutton-to-ikimagebrowsercell – wmorrison365 2012-01-16 15:14:15