Reputation: 1137
I want to make a circle which can be cut by percent, for example, 0.25 is 1/4 of the circle.
This is my code so far:
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)
In startAngle I wrote CGFloat(M_PI + M_PI_2)
because the start of the circle doesnt start in 0 but as in this picture:
In the endAngle
line I wrote CGFloat(90 * M_PI / 180)
because the formula from degrees to radians is: degrees*pi/180 .
BUT! In the view I get half of the circle and not quarter of the circle:
Does my endAngle formula is wrong?
Upvotes: 0
Views: 264
Reputation: 1137
Because in iOS the top of the circle starts at 270 degrees, the startAngle is:
startAngle: CGFloat(M_PI + M_PI_2)
And the endAngle is:
endAngle:CGFloat((360 * 0.25 - 90)
Instead of 0.25, you can change it to any number between 0 to 1 (for example: if you want half of the circle to be full, change it to 0.5)
Upvotes: 0
Reputation: 1567
I think what you want is for your start angle to be 0 and your end angle to be 90 * PI/180
What you have now is a curve that starts at the bottom 270, aka M_PI + M_PI/2 and goes to 90 (90 * M_PI / 180)
//Possibly what you want is for your endAngle = (90 * M_PI/180) + startAngle , aka a quarter more than where you started, i'm not sure which quarter of the circle you want
Upvotes: 2