Search code examples
iosswiftcore-graphicsshadowuibezierpath

Line circle with shadow swift


I created line circle with shadow, but when I tried to change shadowRadius, the shadow was not showing

let path = UIBezierPath(ovalIn: rect)
let shapeLayer              = CAShapeLayer()
shapeLayer.path             = path.cgPath
shapeLayer.fillColor        = UIColor.clear.cgColor
shapeLayer.strokeColor      = UIColor.white.cgColor
shapeLayer.lineWidth        = 1
shapeLayer.shadowOpacity    = 0.9
shapeLayer.shadowColor      = UIColor.white.cgColor
shapeLayer.shadowOffset     = .zero
shapeLayer.shadowRadius     = 10.0
//shapeLayer.shadowPath       = path.cgPath

layer.addSublayer(shapeLayer)

line circle with default corner radius

line circle with corner radius 10.0

Can someone help me?


Solution

  • If you want to create the effect off a "stronger" shadow, you can do so by using a shadow path that has been stroked to be larger than the path that's "casting the shadow". This can be achieved using copy(strokingWithWidth:lineCap:lineJoin:miterLimit:transform:):

    shapeLayer.shadowPath = path.cgPath.copy(strokingWithWidth: width, lineCap: .round, lineJoin: .round, miterLimit: 0)
    

    This is what such a shadow (in red) looks like, both with and without any blur (shadow radius):

    stroked shadow path with and without blur

    You can configure the shadowBlur and the width of the stroked shadow path to achieve the effect that you're after. Below are some examples:

    A few variations of shadow radius and shadow path stroke width