Principle of image display

1. Difference between UIView and CALayer

View is the layer’s proxy object; View is responsible for managing layer, layer is responsible for rendering;

View initialization creates a layer by default; Setting the view’s frame and bounds inside actually changes the layer properties.

  1. The most obvious difference between the two is that a View can accept and handle events, while a Layer cannot.
  2. All UIKit classes that derive from UIView inherit directly or indirectly from UIResponder, so they all have response events.
  3. CALayer directly inherits NSObject and has no interface for handling events.

Conclusion:

  1. Each UIView has a CALayer behind it to draw and display the content, and the size styles of UIView are provided by the Layer inside. Both have tree hierarchies. The layer has SubLayers and the View SubViews, but the layer has more anchor points than the View
  2. When a View is displayed,UIView acts as a Layer’s CALayerDelegate, and the View is displayed by the internal CALayer’s display.
  3. The CALayer property is modified to support hermitage animation by default. When animating UIView’s Layer, the View acts as a proxy for the Layer, and the Layer requests the corresponding action from the View via actionForLayer: forKey:
  4. PresentLayer Tree(animation Tree) modeLayer Tree(model Tree) Render Tree(Render Tree) When doing iOS animation, we modify the properties of the animation, the animation is actually the presentLayer property value of the Layer, and the final display on the interface is actually the modelLayer that provides the View
  5. The most obvious difference between the two is that a View can accept and handle events, while a Layer cannot. (CALayer has no response chain and can’t listen for clicks, touches, etc.)

What is the parent class of UIButton? What’s the parent of UILabel?

  • UIButton -> UIControl -> UIView -> UIResponder
  • UILabel -> UIView -> UIResponder

How do I pause an animation that is playing in UIView? How to recover after a pause?

Record time, modify layer speed and timeoffset

What is the relationship between setNeedsDisplay and layoutIfNeeded?

  • SetNeedsDisplay marks the current view.
  • LayoutIfNeeded looks for tokens, and if so refreshes immediately.

UI Drawing Principles