我正在创建圆形填充动画,但我的圆圈填充为80%,50%不是一半,有什么方法可以纠正因为我很困惑 class FillCircleAnimatedView : UIView { private let shapeLayer = CAShapeLayer() override init(frame: CGRect) { s
class FillCircleAnimatedView : UIView { private let shapeLayer = CAShapeLayer() override init(frame: CGRect) { super.init(frame: frame) setUpLayer() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setUpLayer() { shapeLayer.fillColor = UIColor.clear.cgColor shapeLayer.strokeColor = UIColor.black.cgColor shapeLayer.lineWidth = frame.width let startAngle = (-1 * CGFloat.pi) / 2 let endAngle = 2 * CGFloat.pi shapeLayer.path = UIBezierPath(arcCenter: center, radius: frame.width / 2, startAngle: startAngle, endAngle: endAngle, clockwise: true).cgPath shapeLayer.strokeEnd = 0 layer.addSublayer(shapeLayer) } func fillCircle(with progress: CGFloat) { shapeLayer.strokeEnd = progress } }
在50%
80%
它显示了这样的圆,因为您的路径仅从-π/ 2变为2π.减去这两个值得出5π/ 2,这意味着你的路径不只是一个完整的圆.在50%时,内角为225度/1.25π.你的整条路径比整圆宽90度.您错误地设置了起始值和结束值.
参考此图片:
你应该像你一样从-π/ 2开始,但以3π/ 2结束.减去这两个应该得到一个完整的圆 – 2π