iOS: Using UIView's 'drawRect:' vs. its layer's delegate 'drawLayer:inContext:'

后端 未结 6 1665
无人共我
无人共我 2020-12-04 06:36

I have a class which is a subclass of UIView. I am able to draw stuff inside the view either by implementing the drawRect method, or by implementin

6条回答
  •  悲哀的现实
    2020-12-04 06:52

    On iOS, the overlap between a view and its layer is very large. By default, the view is the delegate of its layer and implements the layer's drawLayer:inContext: method. As I understand it, drawRect: and drawLayer:inContext: are more or less equivalent in this case. Possibly, the default implementation of drawLayer:inContext: calls drawRect:, or drawRect: is only called if drawLayer:inContext: is not implemented by your subclass.

    How to decide which approach to use? Is there a use case for each one?

    It doesn't really matter. To follow the convention, I would normally use drawRect: and reserve the use of drawLayer:inContext: when I actually have to draw custom sublayers that are not part of a view.

提交回复
热议问题