UIKitMidMCQ
What is `CALayer` and how does it relate to `UIView`?
Test your knowledge:
Explanation & Code
Answer:
Every UIView has a CALayer that does the actual drawing and compositing. UIView is a wrapper that adds event handling on top of CALayer.
Code Example:
let view = UIView()
// Common layer properties
view.layer.cornerRadius = 12
view.layer.borderWidth = 1
view.layer.borderColor = UIColor.systemBlue.cgColor
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOpacity = 0.2
view.layer.shadowRadius = 4
view.layer.shadowOffset = CGSize(width: 0, height: 2)
// Clip to bounds — apply on both for correctness
view.clipsToBounds = true // UIView level
view.layer.masksToBounds = true // CALayer level
// CALayer animations — lower level than UIView.animate
let animation = CABasicAnimation(keyPath: "opacity")
animation.fromValue = 1.0
animation.toValue = 0.0
animation.duration = 0.3
view.layer.add(animation, forKey: "fade")
Key distinction: UIView is on the main thread; CALayer can render on background threads.
Rate your understanding: