Swift 3' CGContextAddArc'不可用:使用addArc(center:radius:startAngle:endAngle:clockwise :)

时间:2017-03-06 03:30:00

标签: swift

我在这里有这行代码:

CGContextAddArc(context, (round(frame.size.width))/(2 * (appDelegate.webview?.scrollView.zoomScale)!), round(frame.size.height)/(2 * (appDelegate.webview?.scrollView.zoomScale)!), (round(frame.size.width) - 10)/(2 * (appDelegate.webview?.scrollView.zoomScale)!), 0.0, CGFloat(M_PI * 2.0), 1)

但是你现在得到这个错误:

'CGContextAddArc' is unavailable: Use addArc(center:radius:startAngle:endAngle:clockwise:)

所以我尝试使用addArc但是当我开始输入时,该方法没有出现,我如何调用addArc?

这是我的完整代码:

func drawCircle()
    {
        // Get the Graphics Context
        let context = UIGraphicsGetCurrentContext();

        // Set the circle outerline-width
        context?.setLineWidth(5.0);

        // Set the circle outerline-colour
        UIColor.red.set()

        // Create Circle
        CGContextAddArc(context, (round(frame.size.width))/(2 * (appDelegate.webview?.scrollView.zoomScale)!), round(frame.size.height)/(2 * (appDelegate.webview?.scrollView.zoomScale)!), (round(frame.size.width) - 10)/(2 * (appDelegate.webview?.scrollView.zoomScale)!), 0.0, CGFloat(M_PI * 2.0), 1)

        // Draw
        context?.strokePath();
    }

2 个答案:

答案 0 :(得分:4)

let center = CGPoint(x:(round(frame.size.width))/(2 * (appDelegate.webview?.scrollView.zoomScale)!), y:round(frame.size.height)/(2 * (appDelegate.webview?.scrollView.zoomScale)!))
let radius = (round(frame.size.width) - 10)/(2 * (appDelegate.webview?.scrollView.zoomScale)!)

context.addArc(center: center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI * 2.0), clockwise: true)

答案 1 :(得分:0)

这是绘制弧线的Objective-C语法。

CGContextAddArc(CGContextRef _Nullable c,CGFloat x,CGFloat y,CGFloat半径,CGFloat startAngle,CGFloat endAngle,int顺时针>)

调用方法:CGContextAddArc(context,x,y,40,0.0,M_PI * 2.0,是);

但是在swift中,语法更简单,删除了上下文,并用center替换了x y坐标。

context?.addArc(中心:CGPoint,半径:CGFloat,startAngle:CGFloat,endAngle:CGFloat,顺时针:Bool)

context?.addArc(center: center, radius: 40, startAngle: 0.0, endAngle: CGFloat(.pi * 2.0), clockwise: true)

要计算中心使用以下行。

let x = (self.bounds.origin.x + self.bounds.size.width) / 2
let y = (self.bounds.origin.y + self.bounds.size.height) / 2
let center = CGPoint(x: x, y: y)
   

添加fillEllipse行。

context?.fillEllipse(in: rect)