加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 如何在Mac上与图层支持的视图进行交互

发布时间:2020-12-16 06:56:17 所属栏目:百科 来源:网络整理
导读:我正在设计一个包含多个标签和文本字段的用户界面.我想像这样设置UI: 为我的NSWindow的内容视图设置背景图案 在左上角的背景中添加自定义图标 我通过使内容视图成为层支持的视图解决了第一个问题,如Apple’s documentation of NSView 中所述: A layer-back
我正在设计一个包含多个标签和文本字段的用户界面.我想像这样设置UI:

>为我的NSWindow的内容视图设置背景图案
>在左上角的背景中添加自定义图标

我通过使内容视图成为层支持的视图解决了第一个问题,如Apple’s documentation of NSView中所述:

A layer-backed view is a view that is backed by a Core Animation layer. Any drawing done by the view is the cached in the backing layer. You configured a layer-backed view by simply invoking setWantsLayer: with a value of YES. The view class will automatically create the a backing layer for you,and you use the view class’s drawing mechanisms. When using layer-backed views you should never interact directly with the layer.

A layer-hosting view is a view that contains a Core Animation layer that you intend to manipulate directly. You create a layer-hosting view by instantiating an instance of a Core Animation layer class and setting that layer using the view’s setLayer: method. After doing so,you then invoke setWantsLayer: with a value of YES. When using a layer-hosting view you should not rely on the view for drawing,nor should you add subviews to the layer-hosting view.

然后从绘制我的CGImage的CGPattern生成CGColorRef:

NSView *mainView = [[self window]contentView];
[mainView setWantsLayer:YES];

要将背景图像设置为模式,我在这里使用了How to tile the contents of a CALayer的答案,以完成第一项任务.

但是对于第二个任务,添加图标我使用下面的代码:

CGImageRef iconImage = NULL;
NSString *path = [[NSBundle mainBundle] pathForResource:@"icon_128" ofType:@"png"];
if(path != nil) {
    NSURL *imageURL = [NSURL fileURLWithPath:path];
    provider = CGDataProviderCreateWithURL((CFURLRef)imageURL);
    iconImage = CGImageCreateWithPNGDataProvider(provider,NULL,FALSE,kCGRenderingIntentDefault); 
    CFRelease(provider);
}
CALayer *iconLayer = [[CALayer alloc] init];
// layer is the mainView's layer
CGRect layerFrame = layer.frame;
CGFloat iconWidth = 128.f;
iconLayer.frame = CGRectMake(0.f,CGRectGetHeight(layerFrame)-iconWidth,128.f,128.f);
iconLayer.contents = (id)iconImage;
CGImageRelease(iconImage);
[layer insertSublayer:iconLayer  atIndex:0];
[iconLayer release];

问题

>我不确定我是否违反Apple对图层支持视图的限制,您不应该直接与图层进行交互.设置图层的背景颜色时,我正在与图层直接交互,或者我在这里弄错了?
>我对直接与图层支持的视图的图层层次结构进行交互并插入一个像我为第二个任务所做的新图层感觉不好.这可能还是违反Apple的指导方针?我想指出,这个内容视图当然有几个子视图,如标签,文本视图和按钮.
>在我看来,只使用一个单层托管NSView似乎是最干净的解决方案.然后可以将所有文本标签添加为CATextLayers等.但是,如果我正确理解Apple的文档,则无法再向视图添加任何控件.我是否必须在自定义CALayers中编写所有控件以使其正常工作?听起来像是重新发明了豪华的轮子.我也不知道如何仅在CoreAnimation中编写NSTextField代码.

关于如何使用CoreAnimation和标准控件分割设计用户界面的任何建议都值得赞赏.

请注意,我在这里谈论的是Mac.

解决方法

没有层支持需要恕我直言:

为1.我做一个模式图像

NSImage *patternImage = [NSImage imageNamed:@"pattern"];
[window setBackgroungdColor:[NSColor colorWithPatternImage:patternImage]];

for 2.添加NSImageView作为contentview的子视图

NSImageView *v = ...
[[window contentView] addSubview:v];

在mac上一些视图不能很好地响应IF层支持::例如pdfview

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读