Using CALayer Delegate

前端 未结 8 1113
滥情空心
滥情空心 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:14

    The lightest-wight solution would be to create a small helper class in the the file as the UIView that's using the CALayer:

    In MyView.h

    @interface MyLayerDelegate : NSObject
    . . .
    @end
    

    In MyView.m

    @implementation MyLayerDelegate
    - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx
    {
    . . .
    }
    @end
    

    Just place those at the top of your file, immediately below the #import directives. That way it feels more like using a "private class" to handle the drawing (although it isn't -- the delegate class can be instantiated by any code that imports the header).

提交回复
热议问题