【发布时间】:2017-03-13 23:19:31
【问题描述】:
我目前正在做一个需要绘制饼图的项目。我没有使用第三方库,而是尝试使用核心图形来绘制它。这是绘制饼图的代码。
let circlePath = UIBezierPath(arcCenter: CGPoint(x: self.frame.width/2 + r/8, y: self.frame.height/2 + r/8), radius: r, startAngle: CGFloat(0), endAngle: CGFloat(M_PI * 2 * Double(percent1 / 100)), clockwise: true)
let circlePath2 = UIBezierPath(arcCenter: CGPoint(x: self.frame.width/2 + r/8, y: self.frame.height/2 + r/8), radius: r, startAngle: CGFloat(M_PI * 2 * Double(percent1 / 100)), endAngle: CGFloat(0), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
let shapeLayer2 = CAShapeLayer()
shapeLayer2.path = circlePath2.cgPath
//change the fill color
shapeLayer.fillColor = UIColor.red.cgColor
shapeLayer2.fillColor = UIColor.blue.cgColor
//you can change the stroke color
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer2.strokeColor = UIColor.blue.cgColor
//you can change the line width
shapeLayer.lineWidth = 3.0
self.layer.addSublayer(shapeLayer)
self.layer.addSublayer(shapeLayer2)
但是,这不会产生预期的效果,因为它是线性绘制圆而不是围绕中心绘制的。
【问题讨论】:
-
通过使用其中一个awesome-ios-chart
-
从两个中心坐标中删除
+ r/8。