我尝试在
CAGradientLayer
之上添加一个CAShapeLayer
,但我无法在不弄乱ShapeLayer
位置的情况下获得GradientLayer
的全帧,因此边缘似乎被剪裁了。请参阅下面的照片。
与
GradientLayer
:
没有
GradientLayer
:
这是创建
ShapeLayer
的代码:
func createCircularPath() {
let circularPath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: frame.size.width / 2, startAngle: startPoint, endAngle: endPoint, clockwise: true)
progressLayer.path = circularPath.cgPath
progressLayer.fillColor = UIColor.clear.cgColor
progressLayer.lineCap = .round
progressLayer.lineWidth = 8
progressLayer.strokeEnd = 0
progressLayer.strokeColor = UIColor.white.cgColor
layer.addSublayer(progressLayer)
}
这是创建
GradientLayer
的代码:
func createGradientPath() {
gradientLayer.type = CAGradientLayerType.conic
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 0)
gradientLayer.colors = [UIColor.yellow.cgColor, UIColor.red.cgColor]
gradientLayer.frame = self.bounds
gradientLayer.mask = progressLayer
layer.addSublayer(gradientLayer)
}
猜测,您需要根据线宽插入渐变图层的框架,因此使用
self.bounds.insetBy(dx: -lineWidth, dy: -lineWidth)
作为渐变视图的框架。
这应该在渐变图层的所有 4 个边缘上添加 lineWidth/2 空间。