NSView中CALayers的简单示例 [英] Simple example of CALayers in an NSView

查看:64
本文介绍了NSView中CALayers的简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向NSView添加多个CALayers,但显示时我的视图仍然为空:

I am trying to add several CALayers to an NSView but my view remains empty when shown:

这是我的代码:

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {

    self.layer = [CALayer layer];
    self.wantsLayer = YES;
    CALayer *newLayer = [CALayer layer];
    NSImage *image = [NSImage imageNamed:@"page.png"];
    newLayer.backgroundColor = [NSColor redColor].CGColor;
    newLayer.contents = (id)[image CGImageForProposedRect:NULL context:NULL hints:nil];
    newLayer.frame = NSMakeRect(100,100,100,100);//NSMakeRect(0,0,image.size.width,image.size.height);
    newLayer.position  = CGPointMake(20,20);
    [self.layer addSublayer:newLayer];

}
return self;

}

DO有任何想法(或代码示例)来执行此任务?

DO have any idea (or example of code) to perform this task?

感谢和问候,

推荐答案

设置层的代码需要在awakeFromNib方法中,而不是在initWithFrame函数中。

The code to set up the layer needs to be in the awakeFromNib method, not in the initWithFrame function.

原因说明:)

在nib文件中,您的视图被标记为不需要图层,因此流程为

In the nib file your view is marked as not needing a layer, so the flow is


  • 您可以在initWithFrame:方法中设置图层

  • 设置nib文件属性以擦除图层。

您还可以按原样保留代码,并告诉界面构建器您的自定义视图需要一层。

You could also leave your code the way it is, and tell interface builder that your custom view needs a layer.

这篇关于NSView中CALayers的简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆