本文介绍了如何在Mac上与支持图层的视图进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个包含几个标签和文本字段的用户界面.我想这样设计用户界面:

I am designing a user interface containing several labels and text fields. I would like to style the UI like this:

  1. 为我的NSWindow
  2. 的内容视图设置背景图案
  3. 在左上角的背景中添加自定义图标
  1. setting a background pattern for the content view of my NSWindow
  2. adding a custom icon to the background in the upper left corner

我解决了第一个问题,方法是使内容视图成为层支持的视图,如 Apple的NSView 文档:

I solved the first problem by making the content view a layer-backed view as described in Apple's documentation of NSView:

图层宿主视图是一个包含您打算直接操作的核心动画"图层的视图.您可以实例化Core Animation图层类的实例,然后使用该视图的setLayer:方法设置该图层,从而创建一个图层托管视图.这样做之后,您便可以使用值YES调用setWantsLayer:. 使用图层宿主视图时,您不应依赖该视图进行绘制,也不应在该图层宿主视图中添加子视图.

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.

,然后从CGPattern中生成一个CGColorRef,从而绘制了我的CGImage:

and then generating a CGColorRef out of a CGPattern which draws my CGImage:

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

要将背景图像设置为图案,我使用了如何在SO上平铺CALayer的内容以完成第一个任务.

To set the background image as a pattern I used the answer from How to tile the contents of a CALayer here on SO to get the first task done.

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

However for the second task, adding the icon I used the code below:

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

问题

  1. 我不确定我是否违反Apple关于图层支持的视图的限制,即永远不要直接与图层交互.设置图层的背景色时我是直接与图层进行交互还是在这里弄错了?
  2. 我不喜欢直接与 layer-backed 视图的层层次结构进行交互,并像执行第二个任务一样插入新的层.这可能还是违反了苹果的指导方针?我想指出的是,该内容视图当然具有几个子视图,例如标签,文本视图和按钮.
  3. 在我看来,仅使用一个 layer-hosting NSView似乎是最干净的解决方案.然后可以将所有文本标签添加为CATextLayers等.但是,如果我正确理解Apple的文档,则无法再向视图添加任何控件.我是否必须在自定义CALayers中自己编写所有控件的代码才能使其正常工作?听起来像是重新发明了车轮 deluxe .我也不知道如何只用CoreAnimation编写NSTextField.
  1. I am not sure if I am violating Apple's restrictions concerning layer-backed views that you should never interact directly with the layer. When setting the layer's background color I am interacting directly with the layer or am I mistaken here?
  2. I have a bad feeling about interacting with the layer hierarchy of a layer-backed view directly and inserting a new layer like I did for my second task. Is this possible or also violating Apple's guidelines? I want to point out that this content view of course has several subviews such as labels, a text view and buttons.
  3. It seems to me that just using one single layer-hosting NSView seems to be the cleanest solution. All the text labels could then be added as CATextLayers etc. However if I understand Apple's documentation correctly I cannot add any controls to the view anymore. Would I have to code all the controls myself in custom CALayers to get it working? Sounds like reinventing the wheel de luxe. I also have no idea how one would code a NSTextField solely in CoreAnimation.

关于如何使用CoreAnimation和标准控件进行拆分设计的用户界面的任何建议.

Any advice on how split designing user interfaces with CoreAnimation and standard controls is appreciated.

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

推荐答案

不需要图层支持恕我直言:

no layer backing needed IMHO:

对于1.我制作图案图像

for 1. I do a pattern image

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

在2.中添加一个NSImageView作为contentview的子视图

for 2. add an NSImageView as a subview of the contentview

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


在Mac上,如果支持图层,某些视图不能很好地响应::例如pdfview


on mac some views dont respond nicely IF layer backed:: e.g. pdfview

这篇关于如何在Mac上与支持图层的视图进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:04