ios - 如何在swift中从第一象限分割一个圆

标签 ios swift swift2 core-graphics

基本上我想创建一个计时器应用程序。这就是为什么我需要一个圆圈来显示计时器的进度。如果定时器是 10 秒,圆圈将被分成 10 段,每段将在一秒后出现。好的。

我已经从SO post -> draw-a-circular-segment-progress-in-swift成功创建了它但有点问题。我圈子的部分从第四象限开始,但不是预期的。我想显示第一象限的部分。在这里,我为 8 个段创建了这个函数。

请提供可以动态使用的建议。

 func createCircle(){

        let circlePath = UIBezierPath(arcCenter: CGPoint(x: view.frame.size.width/2,y: view.frame.size.height/2), radius: CGFloat(90), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)


        let segmentAngle: CGFloat = (360 * 0.125) / 360

        for i in 0 ..< 8 {

            let circleLayer = CAShapeLayer()
            circleLayer.path = circlePath.CGPath

            // start angle is number of segments * the segment angle
            circleLayer.strokeStart = segmentAngle * CGFloat(i) //I think all is for this line of code.

            print("\nStroke \(i): ",segmentAngle * CGFloat(i))

            // end angle is the start plus one segment, minus a little to make a gap
            // you'll have to play with this value to get it to look right at the size you need
            let gapSize: CGFloat = 0.008

            circleLayer.strokeEnd = circleLayer.strokeStart + segmentAngle - gapSize

            circleLayer.lineWidth = 10

            circleLayer.strokeColor = UIColor(red:0,  green:0.004,  blue:0.549, alpha:1).CGColor
            circleLayer.fillColor = UIColor.clearColor().CGColor

            // add the segment to the segments array and to the view
            segments.insert(circleLayer, atIndex: i)

        }

        for i in 0 ..< 8{
            view.layer.addSublayer(segments[i])
        }
    }

最佳答案

您必须偏移您的起始角度:

circleLayer.strokeStart = segmentAngle * CGFloat(i) - M_PI / 2.0

虽然转换也可以解决问题,但我不建议在这种情况下使用转换。如果您以后出于其他原因(动画等)想要进行转换,那么您可能还必须考虑任何当前的转换,这可能会使事情变得更加复杂。

编辑:

我认为您的其他一些代码也可能已关闭。我没有弄清楚发生了什么,而是继续从头开始重写它(使用 Swift 3 语法):

let count: Int = 8
let gapSize: CGFloat = 0.008
let segmentAngleSize: CGFloat = (2.0 * CGFloat(M_PI) - CGFloat(count) * gapSize) / CGFloat(count)
let center = CGPoint(x: view.frame.size.width / 2.0, y: view.frame.size.height / 2.0)
let radius: CGFloat = 90
let lineWidth: CGFloat = 10
let strokeColor = UIColor(red:0,  green:0.004,  blue:0.549, alpha:1).cgColor

for i in 0 ..< count {
    let start = CGFloat(i) * (segmentAngleSize + gapSize) - CGFloat(M_PI / 2.0)
    let end = start + segmentAngleSize
    let segmentPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: start, endAngle: end, clockwise: true)

    let arcLayer = CAShapeLayer()
    arcLayer.path = segmentPath.cgPath
    arcLayer.fillColor = UIColor.clear.cgColor
    arcLayer.strokeColor = strokeColor
    arcLayer.lineWidth = lineWidth

    self.view.layer.addSublayer(arcLayer)
}

example code screenshot

关于ios - 如何在swift中从第一象限分割一个圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40700003/

相关文章:

ios - 如何通过多个 View 进行搜索?

Swift 使用解除警报消息

swift - 如何设置 Swift 2.0 路径?

ios - 无法在 iOS 中配置 facebook LoginButtonDelegate

iOS 我可以将所有委托(delegate)方法放在另一个类上吗?将委托(delegate)继承到 View Controller 中

ios - 带有自动反转的 UIView 动画

ios - Json Values Append Array 但 ViewDidLoad 看起来是空的

ios - 我真的需要将我的项目货币转换为 NSDecimalNumber 吗?

ios - 如何在 RXSwift 中重置 Observable 间隔运算符?

ios - UIGraphicsGetCurrentContext 没有出现在 UIView 中