我想做一个可以按百分比切割的圆,比如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)
因为从度数到弧度的公式是:degrees*pi/180。
但是!在 View 中,我得到了一半的圆而不是四分之一的圆:
我的 endAngle 公式是否有误?
最佳答案
我想你想要的是你的开始角度是 0,你的结束角度是 90 * PI/180
您现在拥有的是一条从底部 270 开始的曲线,也就是 M_PI + M_PI/2 到 90 (90 * M_PI/180)
//可能你想要的是你的 endAngle = (90 * M_PI/180) + startAngle ,也就是比你开始的地方多四分之一,我不确定你想要圆的哪个四分之一
关于ios - 在圆上使用 UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37076211/