问题:
我实现了一个可折叠的标题视图,其中包含一个弯曲的底部边框和一个位于我的scrollView上方的渐变图层。标题有一个阴影,它在draw(rect :)函数中绘制。阴影使底部边框具有发光效果。一切看起来都不错,但是当我的标题上调用setNeedsDisplay()或重绘视图时,投影会消失。
问题:
重绘视图时,如何使投影保持不变?
代码:
git commit
答案 0 :(得分:0)
Aight能够解决这个问题。重绘视图时,bezier路径阴影被隐藏,因为图层蒙版被设置为shapeLayer。我的解决方案是使用图层的shadowPath
属性绘制弯曲阴影,并将shapeLayer
作为子层插入而不是屏蔽图层。确保在添加渐变之前添加shapeLayer
,因此渐变显示在shapeLayer
的顶部。正如@dfd所提到的那样,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
}