作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要在 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 开始的)
最佳答案
您应该设置适当的CGLineCap
。你有 CGLineCap.square
,但你需要 CGLineCap.butt
这里有更多信息:CGLineCap
layer.lineCap = CAShapeLayerLineCap.square
layer.lineCap = CAShapeLayerLineCap.butt
关于ios - 圆形贝塞尔曲线路径不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53044700/
我是一名优秀的程序员,十分优秀!