Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBezierPath addClip and drawRect

I know there is a method to draw a roundedRect - UIBezierPath(roundedRect, cornerRadius)

But I would like to know if I clip the corner by myself, why should I have to addClip before draw a rectangle? (I feel like clip the rectangle after it was draw is more reasonable. 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) 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()
}
like image 725
Jenny Avatar asked Sep 15 '15 09:09

Jenny


1 Answers

If you imagine the drawing area as a sheet of paper, clipping does not mean that you cut something off it with a scissor after drawing. Instead it is more like putting a stencil board on the sheet and only the punched-out areas are relevant for drawing then.

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 drawings.

like image 88
iOSX Avatar answered Nov 12 '22 17:11

iOSX