I'm trying to draw a simple circle when I get to the following line I get the error "Double is Not Convertable to CGFloat under the startAngle = 0.0
path.addArcWithCenter(center, radius: radius, startAngle: 0.0, endAngle: Float(M_PI) * 2.0, clockwise: true)
How do I "cast" 0.0 to make it CGFloat in Swift?
The complete function I am writing:
func drawCircle() { // Drawing code var bounds:CGRect = secondView.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: CGFloat(0.0), endAngle: Float(M_PI) * 2.0, clockwise: true) path.stroke() }
Suggested approach: It's a question of how many bits are used to store data: Float is always 32-bit, Double is always 64-bit, and CGFloat is either 32-bit or 64-bit depending on the device it runs on, but realistically it's just 64-bit all the time.
Swift version: 5.6. A CGFloat is a specialized form of Float that holds either 32-bits of data or 64-bits of data depending on the platform. The CG tells you it's part of Core Graphics, and it's found throughout UIKit, Core Graphics, Sprite Kit and many other iOS libraries.
Convert the values that need to be CGFloat to a CGFloat.
path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(M_PI) * 2.0, clockwise: true)
startAngle probably shouldn't need to be converted though if you're just passing a literal. Also note that this isn't a C style cast, but actually converting between different Swift Types.
Edit: Looking at your whole function, this works.
func drawCircle() { // Drawing code var bounds:CGRect = self.view.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: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(Float(M_PI) * 2.0), clockwise: true) path.stroke() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With