将 NSControl 添加到 IKImageBrowserCell
我构建了一个自定义 IKImageBrowserCell,它可以在 IKImageBrowser 中显示我的图像,没有任何问题。
我想尝试覆盖内置的 IKImageBrowser 删除图像功能。目前“开箱即用”,您可以选择一个图像或多个图像,然后按退格键删除。
我想添加一个 NSButton 或类似的按钮以在每个图像上启用相同的功能。
我添加了以下代码,以便在选择 IKImageBrowserCell 时在 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: 方法?我被卡住了!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
myIKImageBrowserView.selectionIndexes() 返回当前选定单元格的 NSIndexSet - 您可以在删除图层之前使用它来调用removeItemsFromIndex方法
https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/IKImageBrowserView/index.html
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