Using CALayer Delegate

前端 未结 8 1104
滥情空心
滥情空心 2020-12-02 08:44

I have a UIView whose layers will have sublayers. I\'d like to assign delegates for each of those sublayers, so the delegate method can tell the layer what to draw. My que

8条回答
  •  情话喂你
    2020-12-02 09:11

    It's possible to implement a delegation without resorting to a strong ref.

    NOTE: The basic concept is that you forward the delegate call to a selector call

    1. Create a selector instance in the NSView you want to get the delegation from
    2. implement the drawLayer(layer,ctx) in the NSView you want to get the delegation from call the selector variable with the layer and ctx vars
    3. set the view.selector to a handleSelector method where you then retrieve the layer and ctx (this can be anywhere in your code, weak or strongly referenced)

    To see an example of how you implement the selector construction:(Permalink) https://github.com/eonist/Element/wiki/Progress#selectors-in-swift

    NOTE: why are we doing this? because creating a variable outside methods whenever you want to use the Graphic class is non-sensical

    NOTE: And you also get the benefit that the receiver of the delegation doesn't need to extend NSView or NSObject

提交回复
热议问题