简体   繁体   中英

Why draw(_ layer: CALayer, in ctx: CGContext) is not called without empty draw(_ rect: CGRect)?

When i override draw(_ rect: CGRect) and leave it empty, then it happens that draw(_ layer: CALayer, in ctx: CGContext) got called. And circle is painted.

在此处输入图像描述

Here is the code. I just assigned SpecialView class to view in IB.

class SpecialView : UIView {
   override func draw(_ rect: CGRect) {
   }
  
   override func draw(_ layer: CALayer, in ctx: CGContext) {
            UIGraphicsPushContext(ctx)
            let p = UIBezierPath(ovalIn: CGRect(0,0,100,100))
            UIColor.blue.setFill()
            p.fill()
            UIGraphicsPopContext()
   }

}

But when i do not override draw(_ rect: CGRect) , and just leave the code as below in code snippet:

 class SpecialView : UIView {
 
   override func draw(_ layer: CALayer, in ctx: CGContext) {
            UIGraphicsPushContext(ctx)
            let p = UIBezierPath(ovalIn: CGRect(0,0,100,100))
            UIColor.blue.setFill()
            p.fill()
            UIGraphicsPopContext()
   }
}

The draw(_ layer: CALayer, in ctx: CGContext) function is not called. And circle is not got painted. 在此处输入图像描述

Do you know why?

Layers are very lazy about drawing. They do not draw unless you tell them that they need to. So, you could solve the problem here by explicitly telling the layer setNeedsDisplay .

However, views are a little more proactive. They redraw themselves automatically under certain circumstances, like when they first appear. Since this layer is the underlying layer of a view, you can get the layer to participate in that automatic redrawing if you can get the view to redraw itself automatically.

But (and this is the important part) the runtime, for the sake of efficiency, assumes that if a view has no drawRect implementation, it does not need automatic redrawing.

So there's your answer:

  1. Normally, a layer would not redraw at all until you tell it to.

  2. By providing a drawRect implementation, even though it is empty, you tell the view to redraw itself automatically when needed.

  3. This is the view's underlying layer, so it is redrawn with the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM