I have a circle added to CAShapeLayer
and a line with UIBeizerPath
. I want to maintain the position of line to be above circle. Basically in drawRect
, position of the items will be maintained one after the other. So, I call drawCircle()
first and invoke drawLine()
expecting that line has to be on top of the circle. Here is my code.
class DrawView: UIView {
override func draw(_ rect: CGRect) {
drawCircle()
drawLine()
}
private func drawCircle() {
if let sublayers = layer.sublayers {
for layer in sublayers where layer.name == "Circle" {
layer.removeFromSuperlayer()
}
}
let circlePath = UIBezierPath(arcCenter: CGPoint(x: bounds.midX, y: 400),
radius: 20.0,
startAngle: 0,
endAngle: CGFloat(Double.pi * 2),
clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.name = "Circle"
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 3.0
layer.addSublayer(shapeLayer)
}
private func drawLine() {
let path = UIBezierPath()
path.move(to: CGPoint(x:bounds.midX, y:0))
path.addLine(to: CGPoint(x: bounds.midX, y: 400))
path.close()
UIColor.blue.set()
path.lineWidth = 20.0
path.stroke()
}
}
and below is the result
But, adding the line to another CAShapeLayer
gives the expected result, adds the line on top of circle. How can I bring the line on top of circle without adding the line to a CAShapeLayer
.