How to add a CALayer to an NSView on Mac OS X

前端 未结 4 417
灰色年华
灰色年华 2021-02-02 12:37

I\'m trying to learn how to use and implement CALayer in a Mac Objective-C application, but I can\'t seem to probably do the most basic thing - add a new layer and

4条回答
  •  我在风中等你
    2021-02-02 12:52

    You need to make a call to the "setWantsLayer" method.

    Check out the following documentation for the description for setWantsLayer: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html

    In a nutshell, your view needs to be layer-hosting view. Because it is a layer-hosting view, you should interact with the layer, and NOT interact with the view itself and don't add subviews to it.

    [self setLayer:[CALayer new]];
    [self setWantsLayer:YES];     // the order of setLayer and setWantsLayer is crucial!
    [self.layer setBackgroundColor:[backgroundColor CGColor]];
    

提交回复
热议问题