gpt4 book ai didi

ios - 圆形贝塞尔曲线路径不正确

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:00 25 4
gpt4 key购买 nike

我需要在 View 中绘制饼图。我已经正确绘制了背景路径。但是当我添加进度层时,路径绘制不正确。

这是我用来创建层并将它们添加到 View 的代码。

我也创建了一个要点。您也可以从那里看到完整的代码。 MultiColoredPie Gist

private lazy var pathLayers: [PieLayer] = {

guard let data = self.data else { return [] }

var previousPercentage: CGFloat = 0
let sorted = data.sorted(by: { (data1, data2) -> Bool in
return data1.percentage < data2.percentage
})

var layers: [PieLayer] = []
for (index, item) in sorted.enumerated() {
previousPercentage += item.percentage
let layer = PieLayer()
layer.index = index
layer.lineCap = CAShapeLayerLineCap.square
layer.fillColor = nil
layer.strokeColor = item.color.cgColor
layer.strokeEnd = previousPercentage //item.percentage
layer.percentage = previousPercentage //item.percentage
layers.append(layer)
}
return layers
}()

override func layoutSubviews() {
super.layoutSubviews()
self.addShapes()
}

func addShapes() {
shapeLayer.path = commonPath.cgPath
shapeLayer.lineWidth = lineWidth
self.layer.addSublayer(shapeLayer)

for layer in pathLayers.reversed() {
print("\(layer.index) - \(layer.percentage)")
layer.lineWidth = lineWidth
layer.path = commonPath.cgPath
self.layer.addSublayer(layer)
}

}

我用这段代码创建路径,

private var commonPath: UIBezierPath {
get {
let x = self.frame.width/2
let y = self.frame.height/2
let center = CGPoint(x: x, y: y)
let path = UIBezierPath(arcCenter: center, radius: CGFloat(x - lineWidth/2), startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true)
path.close()
return path
}
}

有人知道为什么会这样吗? (而且似乎不是从 -90 开始的)

enter image description here

enter image description here

最佳答案

您应该设置适当的CGLineCap。你有 CGLineCap.square,但你需要 CGLineCap.butt

这里有更多信息:CGLineCap

换行

 layer.lineCap = CAShapeLayerLineCap.square

到:

 layer.lineCap = CAShapeLayerLineCap.butt

关于ios - 圆形贝塞尔曲线路径不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53044700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com