NSView and CALayer
I recently come across animation problem in building macOS app. I was not able to display a transformed layer properly. Things went funky. Thus I decided to spend some time in reading the documentation once again.
It brings me to the followings:
They have a relationship like this:
NSView sits inside AppKit Framework, it can become a layer-backed view and add hardware support to render graphics.
- Once CALayer is used, the visual is rendered by hardware. That said, pure NSView is processed by CPU
- NSView has a flipped coordinate system as compared to UIView. Point (0, 0) in NSView refers to bottom-left corner
- AppKit and NSView do not provide easy way to animate content
- Core Animation is actually the same on macOS and iOS. Yet most people on iOS would use UIView to animate
- Core Animation makes use of keyPath to define what to animate
- CATransaction can be used to animate several properties explicitly
- After the animation is done, the animated property will still keep at the original value, thus the visual will jump back at the end of animation
- Set the animated property to the end value after animation code
- Add subview to the view hierarchy before setting anchor point and other transformations
- CATransform itself is a value, not an animation. Set it and the layer will transform to that particular value.
I have worked on a sample project to demonstrate the learnings above through a 3D transform along x-axis.
I am taking a challenge to write a post every day for 180 days. The content will be related to my life as a father, entrepreneur, developer and trainer.
I am developing a Mac App which needs some animations to be done. In fact, I am quite new to animation topics on Mac…medium.com