Pierre Pierre - 3 months ago 11
iOS Question

Line circle with shadow

I'm trying to draw an empty circle with a stroke color with a shadow.

let bezierPath = UIBezierPath(ovalIn: CGRect(x: 80, y: 200, width: 100, height: 100))

let shapeLayer = CAShapeLayer()
shapeLayer.path = bezierPath.cgPath
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.white.cgColor
shapeLayer.lineWidth = 3
shapeLayer.shadowPath = bezierPath.cgPath
shapeLayer.shadowColor = UIColor.black.cgColor
shapeLayer.shadowOffset = CGSize(width: 0, height: 0)
shapeLayer.shadowOpacity = 1

containerView.layer.addSublayer(shapeLayer)


The result

But shadow is filling the entire circle. How to remove it and keep the outer shadow ?

Answer

Just omit setting shapeLayer.shadowPath and you will get something like this (the layer's standard shadow, which is basically the opaque parts of the layer):

enter image description here