使用贝塞尔路径 SWIFT 绘制一个圆



我正在尝试根据日期绘制一个圆,该圆位于我的自定义表视图单元格类中。 例如,我有两个日期对象; 日期 1 和日期 2 分别。日期1越接近日期2,圆应该越完整,成为一个完整的圆。当天数为 0 时,应该有一个完整的圆圈等。

这是我目前所拥有的:

let startAngle = CGFloat(-Double.pi/2)
let endAngle = CGFloat(-Double.pi/2 + Double.pi * Double(2) * Double(components.day!) * 0.1)
let circlePath = UIBezierPath(arcCenter: CGPoint(x: 80,y: 80), radius: CGFloat(60), startAngle:startAngle , endAngle:endAngle, clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
var colour = UIColor.green.cgColor
//change the fill color
shapeLayer.fillColor = UIColor.clear.cgColor
//you can change the stroke color
shapeLayer.strokeColor = colour
//you can change the line width
shapeLayer.lineWidth = 10.0
cell.progressView.layer.addSublayer(shapeLayer)

components.day!是 2 个日期之间的天数。 但是画的圆圈与天数无关。我猜我的角度错了,但不知道为什么

您要求如果日期差 (components.day( 为 20 或更大,则圆圈应为空。如果差值为 0,则圆应该是完整的。因此,您需要做的第一件事是根据components.day计算满百分比。

let percentFull = 1 - Double(min(components.day!, 20)) / 20

然后你可以计算角度:

let startAngle = CGFloat(-Double.pi / 2) // top of circle
let endAngle = startAngle + 2 * Double.pi * percentFull
let circlePath = UIBezierPath(arcCenter: CGPoint(x: 80, y: 80), radius: 60, startAngle: startAngle, endAngle: endAngle, clockwise: true)

相关内容

最新更新