艾希特能够解决这个问题。重新绘制视图时,贝塞尔路径阴影被隐藏,因为层掩码被设置为shapeLayer。我的解决方案是使用层的
shadowPath
属性并插入
shapeLayer
作为子层,而不是掩蔽层。确保添加
形状层
在添加渐变之前,使渐变显示在
这个
didSetGradient
override func draw(_ rect: CGRect) {
super.draw(rect)
let p1 = CGPoint(x: 0, y: self.frame.height * 0.8)
let p2 = CGPoint(x: self.frame.width / 2, y: self.frame.height * 1.06)
let p3 = CGPoint(x: self.frame.width, y: self.frame.height * 0.8)
let p4 = CGPoint(x: self.frame.width, y: 0)
let p5 = CGPoint(x: 0, y: 0)
let path = UIBezierPath()
path.move(to: p1)
path.addQuadCurve(to: p3, controlPoint: p2)
path.addLine(to: p4)
path.addLine(to: p5)
path.addLine(to: p1)
path.close()
self.shapeLayer.path = path.cgPath
if !self.didSetGradient {
self.layer.insertSublayer(shapeLayer, at: 0)
self.gradient = CustomColors.blueGreenBackgroundGradient(frame: path.bounds)
self.gradient.mask = shapeLayer
self.gradient.masksToBounds = true
self.layer.insertSublayer(self.gradient, at: 0)
self.didSetGradient = true
}
self.layer.shadowPath = startPath.cgPath
self.layer.shadowColor = CustomColors.blueGreen.cgColor
self.layer.shadowOpacity = 0.5
self.layer.shadowRadius = 8
self.layer.shadowOffset = CGSize(width: 0, height: 7)
self.layer.masksToBounds = false
self.clipsToBounds = false
}