ios - 在圆上使用 UIBezierPath

标签 ios xcode swift geometry

我想做一个可以按百分比切割的圆,比如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 开始的,而是如图所示:

enter image description here

endAngle我写的行 CGFloat(90 * M_PI / 180)因为从度数到弧度的公式是:degrees*pi/180

但是!在 View 中,我得到了一半的圆而不是四分之一的圆:

enter image description here

我的 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/

相关文章:

xcode - 更改 Interface Builder 文档大纲中约束列表的顺序

ios - 我可以控制两个 View Controller 之间的转换(即通过用户滑动)吗?

ios - 更改 UIImage 中的白色像素

database - Firebase 值(value)没有随 Swift 增加?

ios - 从 Swift 中的蓝牙读取中提取并转换 NSData 值

android - 我口袋里的 IDE?

iOS 应用程序在存档后表现不同

swift - CoreData 或 Outlet 集合的轻微泄漏

ios - 添加到主视图 Controller 时如何访问不同的 subview Controller

javascript - 使用 jQuery 检测移动设备旋转