我尝试将内部阴影添加到使用UIBezierPath绘制的圆上。但是代码似乎不起作用(对于UIView是有效的)。只需要将阴影添加到圆的顶部即可。
func drawCircleWithShadow() {
//Draw Circle
let path = UIBezierPath(arcCenter: CGPoint(x: 100, y: 400), radius: 150, startAngle: 0, endAngle: 6.2831853072, clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
let lightRedColor = UIColor(red: 255.0/255.0, green: 87.0/255.0, blue: 51.0/255.0, alpha: 1)
shapeLayer.strokeColor = lightRedColor.cgColor
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.lineWidth = 2.0
shapeLayer.position = CGPoint(x: 100, y: 100)
self.view.layer.addSublayer(shapeLayer)
let innerShadow = CALayer()
innerShadow.frame = shapeLayer.frame
//Draw Shadow Arc
let shadowPath = UIBezierPath(arcCenter: CGPoint(x: 100, y: 400), radius: 150, startAngle: 3.1415926536, endAngle: 4.7123889804, clockwise: true)
let cutout = shadowPath.reversing()
shadowPath.append(cutout)
innerShadow.shadowPath = shadowPath.cgPath
innerShadow.masksToBounds = true
innerShadow.shadowColor = UIColor.black.cgColor
innerShadow.shadowOffset = CGSize(width: 0, height: 3)
innerShadow.shadowOpacity = 0.15
innerShadow.shadowRadius = 3
shapeLayer.addSublayer(innerShadow)
}