Ios 在swift中的UIViewController中绘制部分圆

Ios 在swift中的UIViewController中绘制部分圆,ios,swift,uiviewcontroller,uikit,Ios,Swift,Uiviewcontroller,Uikit,我想画一个部分圆,在我的UIViewController中有以下函数可以这样做,我在viewDidLoad中调用它: import UIKit class ActivityViewController: UIViewController { let progress = CGRect(origin: CGPoint(x: 200, y: 200), size: CGSize(width: 100, height: 100)) override func viewDidLo

我想画一个部分圆,在我的UIViewController中有以下函数可以这样做,我在viewDidLoad中调用它:

import UIKit

class ActivityViewController: UIViewController {

    let progress = CGRect(origin: CGPoint(x:  200, y: 200), size: CGSize(width: 100, height: 100))

    override func viewDidLoad() {
        super.viewDidLoad()
        drawSlice(rect: progress, startPercent: 0, endPercent: 50, color: UIColor.blue)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func drawSlice(rect: CGRect, startPercent: CGFloat, endPercent: CGFloat, color: UIColor) {
        let center = CGPoint(x: rect.origin.x + rect.width / 2, y: rect.origin.y + rect.height / 2)
        let radius = min(rect.width, rect.height) / 2
        let startAngle = startPercent / 100 * CGFloat(M_PI) * 2 - CGFloat(M_PI)
        let endAngle = endPercent / 100 * CGFloat(M_PI) * 2 - CGFloat(M_PI)
        let path = UIBezierPath()
        path.move(to: center)
        path.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        path.close()
        color.setFill()
        path.fill()
    }


 }

但是当我执行此代码时,圆圈没有出现,我做错了什么?

您必须指定要绘制刚刚创建的路径的位置。例如,您可以使用CAShapeLayer并执行以下操作:

    let myLayer = CAShapeLayer()
    myLayer.path = path.cgPath

    //set some property on the layer
    myLayer.strokeColor = UIColor.blue.cgColor
    myLayer.fillColor = UIColor.white.cgColor
    myLayer.lineWidth = 1.0
    myLayer.position = CGPoint(x: 10, y: 10)

    // add the layer to your view's layer !!important!!
    self.layer.addSublayer(myLayer)

然后你应该看到你的图层

必须指定要绘制刚刚创建的路径的位置。例如,您可以使用CAShapeLayer并执行以下操作:

    let myLayer = CAShapeLayer()
    myLayer.path = path.cgPath

    //set some property on the layer
    myLayer.strokeColor = UIColor.blue.cgColor
    myLayer.fillColor = UIColor.white.cgColor
    myLayer.lineWidth = 1.0
    myLayer.position = CGPoint(x: 10, y: 10)

    // add the layer to your view's layer !!important!!
    self.layer.addSublayer(myLayer)

然后你应该看到你的图层

您需要将绘制作为放置在
UIViewController
中的
UIView
的一部分。一种方法是子类化
UIView
,并在其中添加图形代码:

class MyProgress: UIView {
    // MARK: - Draw

    override func draw(_ rect: CGRect) {

        guard UIGraphicsGetCurrentContext() != nil else {
            return
        }

        let center = CGPoint(x: rect.origin.x + rect.width / 2, y: rect.origin.y + rect.height / 2)
        let radius = min(rect.width, rect.height) / 2
        let startAngle = startPercent / 100 * CGFloat(M_PI) * 2 - CGFloat(M_PI)
        let endAngle = endPercent / 100 * CGFloat(M_PI) * 2 - CGFloat(M_PI)
        let path = UIBezierPath()
        path.move(to: center)
        path.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        path.close()
        color.setFill()
        path.fill()

        context.addPath(path.cgPath)
        context.setFillColor(color.cgColor)
        context.fillPath()
}
然后,可以将此视图添加到ViewController中,并使用希望其显示的约束将其放置


编辑

要查看视图,需要将其添加到ViewController中

这应该是这样的:

import UIKit

class ActivityViewController: UIViewController {

    let progress = CGRect(origin: CGPoint(x:  200, y: 200), size: CGSize(width: 100, height: 100))

    override func viewDidLoad() {
        super.viewDidLoad()
        // instantiate the view
        let progressView = MyProgressView(frame: progress)

        // add it to the view hierarchy
        view.addSubview(progressView)
    }
}

根据您希望此进度视图显示的位置,您可能需要更改框架或将其添加到视图层次结构中的其他位置。

您需要将绘制作为放置在
UIViewController
中的
UIView
的一部分。一种方法是子类化
UIView
,并在其中添加图形代码:

class MyProgress: UIView {
    // MARK: - Draw

    override func draw(_ rect: CGRect) {

        guard UIGraphicsGetCurrentContext() != nil else {
            return
        }

        let center = CGPoint(x: rect.origin.x + rect.width / 2, y: rect.origin.y + rect.height / 2)
        let radius = min(rect.width, rect.height) / 2
        let startAngle = startPercent / 100 * CGFloat(M_PI) * 2 - CGFloat(M_PI)
        let endAngle = endPercent / 100 * CGFloat(M_PI) * 2 - CGFloat(M_PI)
        let path = UIBezierPath()
        path.move(to: center)
        path.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        path.close()
        color.setFill()
        path.fill()

        context.addPath(path.cgPath)
        context.setFillColor(color.cgColor)
        context.fillPath()
}
然后,可以将此视图添加到ViewController中,并使用希望其显示的约束将其放置


编辑

要查看视图,需要将其添加到ViewController中

这应该是这样的:

import UIKit

class ActivityViewController: UIViewController {

    let progress = CGRect(origin: CGPoint(x:  200, y: 200), size: CGSize(width: 100, height: 100))

    override func viewDidLoad() {
        super.viewDidLoad()
        // instantiate the view
        let progressView = MyProgressView(frame: progress)

        // add it to the view hierarchy
        view.addSubview(progressView)
    }
}

根据您希望此进度视图显示的位置,您可能必须更改框架或将其添加到视图层次结构中的其他位置。

感谢您的回答,在上下文行。addPath(fillPath.cgpath)我遇到以下错误:使用未解析标识符“fillPath”我没有发现错误,但圆圈没有出现。你是否在ViewController中向
self.view
添加了
MyProgress
的实例?不,我该怎么做?我已经按照你说的添加了视图,但当我运行代码时,屏幕显示为空白。谢谢你的回答,在context.addPath(fillPath.cgpath)行我得到以下错误:使用未解析标识符“fillPath”我没有得到错误,但圆圈没有出现Gare您是否在ViewController中向
MyProgress
添加
self.view
的实例?不,我该怎么做?我已经按照您所说添加了视图,但当我运行代码时,屏幕显示为空白