Drawing the circle in Swift


#1

I’ve been making my way through the book and using the new Swift language instead of Objective C. The conversion of code in the book to Swift so far has not been too troublesome since all the framework stuff is still the same. However, I had some trouble getting the circle code converted to Swift in the drawRect override. After at least an hour, I finally got it to work with the code below and so wanted to share in case anyone else ran into the same issue.

[code] override func drawRect(rect: CGRect)
{
// Drawing code
var bounds:CGRect = self.bounds
var center = CGPoint()
center.x = bounds.origin.x + bounds.size.width / 2.0
center.y = bounds.origin.y + bounds.size.height / 2.0
var radius = (min(bounds.size.width, bounds.size.height) / 2.0)
var path:UIBezierPath = UIBezierPath()
path.addArcWithCenter(center, radius: radius, startAngle: 0.0, endAngle: Float(M_PI) * 2.0, clockwise: true)
path.stroke()

}[/code]

#2

Did this function work for you?

I’m getting errors “Double is Not Convertable to CGFloat” under the startAngle = 0.0

When I try to cast the startAngle to a CGFloat via CGFloat(0.0) the next error I get is “Type does not conform to protocol FloatLiteralConvertable” for the entire line.