gpt4 book ai didi

MACOSX - 如何自定义 IKImageBrowserView 以在每个项目上添加 NSButton?

转载 作者:行者123 更新时间:2023-12-03 16:41:35 25 4
gpt4 key购买 nike

我想自定义 IKImageBrowserView,以便我可以在 IKImageBrowserView 的单元格上添加 NSButton 或其他控件。我尝试剪切 IKBrowserViewCell 类,但我不知道如何以及在哪里添加 NSbutton,以便它显示在 IKBrowserViewItem 上。这是我到目前为止尝试过的代码。

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

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

细胞类

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

该单元类有方法返回 IKImageBrowserCell 的各种控件的框架,并且不允许添加为 subview 。我没有找到分配 NSButton 的位置,以便我可以在项目上显示。请帮我。我已经在这上面浪费了大约 1 天的时间。任何帮助都是值得赞赏的。谢谢

最佳答案

您正朝着正确的方向前进。您需要检查类型并将按钮添加到前景层。这是我现有项目中的一些代码。

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

关于MACOSX - 如何自定义 IKImageBrowserView 以在每个项目上添加 NSButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18640000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com