Differences in Drawing 2 UIBezierPath

I want to draw the differences between 2 UIBezierPath (see screenshot) to only draw rounded corners, as you can see in my screenshot (Figure C)

enter image description here

Here is my code:

let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)

let rectanglePath = UIBezierPath(rect: rect)
CGContextAddPath(context, rectanglePath.CGPath)
CGContextEOClip(context)

let roundedRectanglePath = UIBezierPath(roundedRect: productRect, byRoundingCorners: roundedCorners, cornerRadii: CGSize(width: 6, height: 6))
CGContextAddPath(context, roundedRectanglePath.CGPath)
CGContextFillPath(context)

CGContextRestoreGState(context)

Unfortunately this will not work. I draw only a rounded black rectangle.

Do you have an idea?

Many thanks.

+4
source share
1 answer

You can use one way:

let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
path.append(UIBezierPath(rect: bounds))
path.usesEvenOddFillRule = true

UIColor.black.setFill()
path.fill()

Or you can use CoreGraphics:

let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
path.append(UIBezierPath(rect: bounds))

let context = UIGraphicsGetCurrentContext()!
context.addPath(path.cgPath)
context.setFillColor(UIColor.black.cgColor)
context.fillPath(using: .evenOdd)

This gives:

enter image description here

See the previous version of this answer for playing Swift 2.

+7
source

Source: https://habr.com/ru/post/1650842/


All Articles