渐变弧视图显示不正确

问题描述 投票:0回答:1

我有一个自定义视图,该视图是渐变颜色的圆(Just Stroke)的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)

}

}

但是边缘被剪裁了

a

我已经尝试了很多,但无法解决。

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

swift gradient calayer cashapelayer
1个回答
0
投票

仅两个更改:

let radius = self.bounds.midX - desiredLineWidth

gradient.frame = self.bounds
© www.soinside.com 2019 - 2024. All rights reserved.