我想制作一个可以按百分比切割的圆,例如0.25是该圆的1/4。
到目前为止,这是我的代码:
let circlePath = UIBezierPath(arcCenter: CGPoint(x: 100,y: 300), radius: CGFloat(20), startAngle: CGFloat(M_PI + M_PI_2), endAngle:CGFloat(90 * M_PI / 180, clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.CGPath
//change the fill color
shapeLayer.fillColor = UIColor.clearColor().CGColor
//you can change the stroke color
shapeLayer.strokeColor = UIColor.redColor().CGColor
//you can change the line width
shapeLayer.lineWidth = 3.0
view.layer.addSublayer(shapeLayer)
我在startAngle中写道,CGFloat(M_PI + M_PI_2)
因为圆的起点不是从0开始,而是如下图所示:
在这endAngle
行中,我写CGFloat(90 * M_PI / 180)
这行是因为从度到弧度的公式是:度* pi / 180。
但!在视图中,我得到的是圆的一半而不是四分之一:
我的endAngle公式是否错误?
因为在iOS中,圆的顶部始于270度,所以startAngle为:
startAngle: CGFloat(M_PI + M_PI_2)
endAngle是:
endAngle:CGFloat((360 * 0.25 - 90)
您可以将其更改为0到1之间的任何数字,而不是0.25(例如:如果您希望圆的一半充满),请将其更改为0.5)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句