user3745888 user3745888 - 1 year ago 54
Swift Question

How to initialize UIBezierPath to draw a circle in swift?

I'm trying to draw a circle in Swift, but when I write my code I get a error "could not find an overload for 'init' that accepts the supplied arguments.

In the class UIBezierPath there is a init function:

init(arcCenter center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool) -> UIBezierPath

But when I declared this with this code I get the error.. Need I cast any variable to other type? but if I compiled this in iphone 4 I don't get the error, only in iphone 5/5s. How can declare this correctly?

let arcCenter = CGPoint(x: CGRectGetMidX(self.bounds), y: CGRectGetMidY(self.bounds))
let radius = Float(min(CGRectGetMidX(self.bounds) - 1, CGRectGetMidY(self.bounds)-1))

let circlePath : UIBezierPath = UIBezierPath(arcCenter: arcCenter, radius: radius, startAngle: -rad(90), endAngle: rad(360-90), clockwise: true)


Answer Source

You need to convert values that are passed as arguments in UIBezierPath 's init method to CGFloat, because Swift sees them as Double or Float (let radius).

 let circlePath : UIBezierPath = UIBezierPath(arcCenter: arcCenter, radius: 
CGFloat(radius), startAngle: CGFloat(-rad(90)), endAngle: CGFloat(rad(360-90)), clockwise: true)