圆形贝塞尔曲线路径不正确
Circular bezier path not in correct path
我需要在视图中绘制饼图。我已经正确绘制了背景路径。但是当我添加进度层时,路径绘制不正确。
这是我用来创建图层并将它们添加到视图的代码。
我也创建了一个要点。您也可以从那里看到完整的代码。
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开始的)
你应该设置正确的CGLineCap
。您有 CGLineCap.square
,但您需要 CGLineCap.butt
这里有更多关于它的信息:CGLineCap
换行
layer.lineCap = CAShapeLayerLineCap.square
至:
layer.lineCap = CAShapeLayerLineCap.butt
我需要在视图中绘制饼图。我已经正确绘制了背景路径。但是当我添加进度层时,路径绘制不正确。
这是我用来创建图层并将它们添加到视图的代码。
我也创建了一个要点。您也可以从那里看到完整的代码。 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开始的)
你应该设置正确的CGLineCap
。您有 CGLineCap.square
,但您需要 CGLineCap.butt
这里有更多关于它的信息:CGLineCap
换行
layer.lineCap = CAShapeLayerLineCap.square
至:
layer.lineCap = CAShapeLayerLineCap.butt