圆线与阴影迅速

时间:2018-07-17 10:26:36

标签: ios swift core-graphics shadow uibezierpath

我创建了带有阴影的直线圆,但是当我尝试更改shadowRadius时,阴影没有显示

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

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

如果要在“更强”的阴影上创建效果,可以通过使用阴影路径将其描画得比“投射阴影”的路径大来实现。可以使用copy(strokingWithWidth:lineCap:lineJoin:miterLimit:transform:)

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

这是这样的阴影(红色),无论有无模糊(阴影半径):

stroked shadow path with and without blur

您可以配置描边阴影路径的shadowBlurwidth以达到所追求的效果。下面是一些示例:

A few variations of shadow radius and shadow path stroke width