I'm trying to draw multiple shapes using UIBezierPath and CAShapeLayer in the same layer so my patternImage color goes across all the shapes correctly, but there seems to be some weird clipping/cutting out that I can't solve. When drawing 2 shapes in the same path & layer it clips the overlap but only when the shapes are not drawn in the same direction.
My code:
let view = UIView()
let path = UIBezierPath()
path.addArc(withCenter: .init(x: 100, y: 100), radius: 100, startAngle: 0 * .pi, endAngle: 2 * .pi, clockwise: true)
path.close()
path.addArc(withCenter: .init(x: 200, y: 100), radius: 100, startAngle: 0 * .pi, endAngle: 2 * .pi, clockwise: true)
path.close()
path.addArc(withCenter: .init(x: 100, y: 300), radius: 100, startAngle: 2 * .pi, endAngle: 0 * .pi, clockwise: false)
path.close()
path.addArc(withCenter: .init(x: 200, y: 300), radius: 100, startAngle: 2 * .pi, endAngle: 0 * .pi, clockwise: false)
path.close()
path.addArc(withCenter: .init(x: 100, y: 500), radius: 100, startAngle: 2 * .pi, endAngle: 0 * .pi, clockwise: false)
path.close()
path.addArc(withCenter: .init(x: 200, y: 500), radius: 100, startAngle: 0 * .pi, endAngle: 2 * .pi, clockwise: true)
let layer = CAShapeLayer()
layer.path = path.cgPath
layer.fillColor = UIColor(patternImage: .maskStripes).cgColor
layer.fillRule = .nonZero
view.layer.addSublayer(layer)
The output: rendered circles
You can see that the first 2 groups of circles that have matching clockwise directions render fine but the last one where the directions are opposites the middle is clipped. Setting the bezierPath's usesEvenOddFillRule doesn't help or the layer's fillRule and I can't guarantee in my real app that the shapes will be drawn with the same directions hence the problem.
Thank you in advance.







I don't think there is a solution that causes all overlapping closed paths to fill the union of their areas. I suggest creating a separate layer for each closed shape you want to fill and adding them all as sublayers. That will work, and let you use separate colors for each (if desired.)