在swift中的UIViewController中绘制一个部分圆

时间:2017-07-21 10:08:45

标签: 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 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()
    }


 }

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

2 个答案:

答案 0 :(得分:3)

您必须指定要绘制刚创建的路径的 where 。例如,您可以使用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)

然后你应该看到你的图层了!

答案 1 :(得分:1)

您需要将UIView作为UIViewController的一部分进行绘制。一种方法是子类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)
    }
}

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