如何以编程方式设置NSView层

Wil*_*sch 4 cocoa calayer

这样做的正确方法是什么?这是我正在尝试的.但是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];
}
Run Code Online (Sandbox Code Playgroud)

DotLayer是CALayer的子类.

Fat*_*tie 9

你有没有忘记setWantsLayer?

contentView.wantsLayer = true
Run Code Online (Sandbox Code Playgroud)

(不要忘记这两行的ORDER是至关重要的......请参阅doco.)

在斯威夫特那是

contentView.wantsLayer = true
Run Code Online (Sandbox Code Playgroud)