如何以编程方式设置NSView的图层(layer)?

4
这应该怎么做呢?这是我尝试的方法,但是dotLayer上的display从未被调用:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    DotLayer *dotLayer = [[DotLayer alloc]init ];
    dotLayer.frame= CGRectMake(10, 10, 100, 100);
    dotLayer.nDots = 4;
    NSView *contentView = window.contentView;
    CALayer *layer = [[CALayer alloc]init];
    layer.frame = CGRectMake(0,0,200,200);
    contentView.layer = layer;
    [layer addSublayer:dotLayer];
    [dotLayer setNeedsDisplay];
}

DotLayer是CALayer的子类。

1个回答

9

你可能忘记设置setWantsLayer了吗?

contentView.wantsLayer = true

那经常是问题的所在!

1
这对我解决了问题。值得注意的是,在Swift中,它是contentView.wantsLayer = true - kbpontius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接