Layer-Backed

1
2
3
4
5
6
7
8
//layer-backed view = false
-(void) drawRect

//layer-backed view = true
//wantsUpdateLayer = true
-(BOOL) wantsUpdateLayer
//wantsUpdateLayer = false
- (void)drawRect:(NSRect)dirtyRect

Layer-Backed Views

By default, AppKit views are not backed by Core Animation layers; layer-backing support has been integrated into AppKit retroactively. But while you never have to worry about this with UIKit, with AppKit there are decisions to make. AppKit differentiates between layer-backed and layer-hosting views, and layer backing can be turned on and off on a per-view-tree basis.

The most straightforward approach to enable layer backing is to set the wantsLayer property to YES on the window’s content view. This will cause all views in the window’s view tree to have their own backing layers, so there’s no need to repeatedly set this property on each individual view. This can be done in code or simply in Interface Builder’s View Effects Inspector.

In contrast to iOS, on the Mac you should treat the backing layers as an implementation detail. This means you should not try to interact with the layers directly, as AppKit owns those layers. For example, on iOS you could simply say:
SELECT ALL

1
self.layer.backgroundColor = [UIColor redColor].CGColor;

But in AppKit, you shouldn’t touch the layer. If you want to interact with the layer in such ways, then you have to go one step further. Overriding NSView’s wantsUpdateLayer method to return YES enables you to change the layer’s properties. If you do this though, AppKit will no longer call the view’s drawRect: method. Instead, updateLayer will be called during the view update cycle, and this is where you can modify the layer.