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

后端 未结 6 1664
无人共我
无人共我 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 07:01

    Here're codes of Sample ZoomingPDFViewer from Apple:

    -(void)drawRect:(CGRect)r
    {
    
        // UIView uses the existence of -drawRect: to determine if it should allow its CALayer
        // to be invalidated, which would then lead to the layer creating a backing store and
        // -drawLayer:inContext: being called.
        // By implementing an empty -drawRect: method, we allow UIKit to continue to implement
        // this logic, while doing our real drawing work inside of -drawLayer:inContext:
    
    }
    
    -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
    {
        ...
    }
    

提交回复
热议问题