渐变弧视图未正确显示

Gradient Arc View Not Showing Correctly

我有一个自定义视图,它是一个带有渐变颜色的圆的 85%(只是描边)。这是我的代码:

class ProfileCircle:UIView
{
override func draw(_ rect: CGRect)
    {
            let desiredLineWidth:CGFloat = 4    // your desired value
            let arcCenter = CGPoint(x: self.bounds.midX, y: self.bounds.midY)

            let radius = self.bounds.midX


        let circlePath = UIBezierPath(
            arcCenter: arcCenter,
            radius: radius,
            startAngle: CGFloat(0),
            endAngle:CGFloat((.pi * 2)*0.85),
            clockwise: true)

        let shapeLayer = CAShapeLayer()
            shapeLayer.lineCap = .round
            shapeLayer.path = circlePath.cgPath
            shapeLayer.fillColor = UIColor.clear.cgColor
            shapeLayer.strokeColor = UIColor.red.cgColor
            shapeLayer.lineWidth = desiredLineWidth

            let gradient = CAGradientLayer()
            gradient.frame = circlePath.bounds
            gradient.colors = [UIColor.colorPrimary.cgColor, UIColor.colorSecondary.cgColor]

        //layer.addSublayer(shapeLayer)
            gradient.mask = shapeLayer
            layer.addSublayer(gradient)

}

}

但是边缘被剪掉了

我已经尝试了很多,但我无法修复它。

另一件事是,当我手动更改半径时,视图不会居中

只有两个变化:

let radius = self.bounds.midX - desiredLineWidth

gradient.frame = self.bounds