Skip to content

Instantly share code, notes, and snippets.

@an0
Last active April 18, 2022 21:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save an0/a635443526dd9d24f8b2 to your computer and use it in GitHub Desktop.
Save an0/a635443526dd9d24f8b2 to your computer and use it in GitHub Desktop.
CALayer Drawing API

Call Chain:

CALayer.display() --- if delegate implements displayLayer ---> CALayerDelegate.displayLayer(:)
|                                                               /
|                                                              /
else                                                          /
|                                                            /
|                                                           |
v                                                           v
CALayer.drawInContext(:) ---> CALayerDelegate.drawLayer(:, inContext:) ---> UIView.drawRect(:)

Quirks, demoed in Pie:

  1. Overriding CALayer.drawInContext(:) or UIView.drawRect(:) causes CALayer.backgroundColor or UIView.backgroundColor to be fill into CGContext, even when backgroundColor is nil(default) in which case black is used. It is different from the default behavior of nil backgroundColor being rendered transparently. Even if superview.backgroundColor is black there will be visual difference. See point 2.
  2. Black background will make a big visual difference from clear blackground in the results of dynamic drawing of animation. Animation with black background results in relatively coarse edges — not only during the animation but also the final drawing.
  3. contentsScale is not always correct: http://www.openradar.me/22595655.
  4. Even though from the call chain it seems UIView.drawRect(:) should be no different from CALayer.drawInContext(:) as the overriding point, it is not true: UIView.drawRect(:) does not support animations while CALayer.drawInContext(:) does.
@alberussoftware
Copy link

The third problem still remains. Just tested on iOS 15.4. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment