UIBezierPath addClip and drawRect

I know that there is a roundedRect drawing method - UIBezierPath(roundedRect, cornerRadius)

But I would like to know if I can fix this corner myself, why should I add addClip before drawing a rectangle? (I feel like we are squeezing a rectangle after it has been drawn more intelligently. What concept did I miss?)

(1) work

 override func drawRect(rect: CGRect) { var clipPath = UIBezierPath(roundedRect: rect, cornerRadius: 8.0) path.addClip() var rectPath = UIBezierPath(rect: rect) UIColor.redColor().setFill() rectPath.fill() } 

(2) does not work

 override func drawRect(rect: CGRect) { var rectPath = UIBezierPath(rect: rect) UIColor.redColor().setFill() rectPath.fill() var clipPath = UIBezierPath(roundedRect: rect, cornerRadius: 8.0) path.addClip() } 
+6
source share
1 answer

If you represent the drawing area as a sheet of paper, clipping does not mean that you are cutting with a pair of scissors after drawing. Instead, it is more like applying a stencil to a sheet, and only perforated areas are important for drawing.

This concept gives you more flexibility because you can remove or exchange the stencil board at any time, i.e. Set a new clipping path, which can be very useful for more complex designs.

+8
source

All Articles