简体   繁体   English

迅速在UIViewController中绘制局部圆

[英]Draw a partial circle in UIViewController in swift

I would like to draw a partial circle and I have the following function to do so in my UIViewController and I call it in viewDidLoad : 我想画一个局部圆,我在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()
    }


 }

But when I execute this code, the circle doesn't appear, what am I doing wrong? 但是,当我执行此代码时,圆环没有出现,我在做什么错?

You must specify where you want to draw the path you just created. 您必须指定吸引你刚刚创建的路径。 For instance, you can use a CAShapeLayer and do something like: 例如,您可以使用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)

And then you should see your layer! 然后,您应该看到您的图层!

You would need to draw as part of a UIView that you place in your UIViewController . 您需要作为放置在UIViewController中的UIView一部分进行绘制。 One way to do this is to subclass UIView and add the drawing code there: 一种方法是子类化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()
}

Then you can add this view to your ViewController and place it, using constraints where you would like it to appear. 然后,您可以使用约束将其添加到ViewController并将其放置在希望出现的位置。


Edit 编辑

To see the view, you need to add it to your ViewController. 要查看视图,您需要将其添加到ViewController。

This would be something like this: 这将是这样的:

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)
    }
}

Depending where you want this progress view to show up, you might have to change the frame or add it to some other place in the view hierarchy. 根据您希望此进度视图显示的位置,您可能必须更改框架或将其添加到视图层次结构中的其他位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM