如何初始化 UIBezierPath 以快速绘制圆圈? [英] How to initialize UIBezierPath to draw a circle in swift?
问题描述
我正在尝试在 Swift 中绘制一个圆,但是当我编写代码时出现错误无法找到接受所提供参数的 'init' 的重载.
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.
在 UIBezierPath 类中有一个 init 函数:
In the class UIBezierPath there is a init function:
init(arcCenter center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool) -> UIBezierPath
但是当我用这段代码声明这个时,我得到了错误..我需要将任何变量转换为其他类型吗?但是如果我在 iphone 4 中编译这个,我不会得到错误,只有在 iphone 5/5s 中.如何正确声明?
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)
谢谢!
推荐答案
您需要将在 UIBezierPath 的 init 方法中作为参数传递的值转换为 CGFloat,因为 Swift 将它们视为 Double 或 Float(让半径).
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)
这篇关于如何初始化 UIBezierPath 以快速绘制圆圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!