2016-05-18 27 views
1

我可以創建一個圈子,但我需要它在「12:00」開始並達到一定比例。如何根據百分比製作圓圈?

let circlePath = UIBezierPath(arcCenter: CGPoint(x: 100,y: 100), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), 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 

    cell.progress.layer.addSublayer(shapeLayer) 
+3

更改開始和結束角度以滿足您的需求。 – rmaddy

回答

4

您需要調整開始角度和結束角度以符合您的標準。

圓圈上的「12:00」將是3pi/2或-pi/2(-M_PI_2)。整圈是2pi(M_PI * 2)。這一百分比當然是M_PI * 2 * percentage。所以最後會是你的開始+這個百分比。

let circlePath = UIBezierPath(arcCenter: CGPoint(x: 100,y: 100), radius: CGFloat(20), startAngle: CGFloat(-M_PI_2), endAngle:CGFloat(M_PI * 2 * percentage - M_PI_2), clockwise: true) 

其中percentage範圍爲0.01.0一些CGFloat