【问题标题】:Draw a partial circle in UIViewController in swift快速在 UIViewController 中绘制部分圆圈
【发布时间】:2017-07-21 10:08:44
【问题描述】:

我想画一个部分圆圈,我在 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()
    }


 }

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

【问题讨论】:

    标签: ios swift uiviewcontroller uikit


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您需要将绘图作为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)
          }
      }
      

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

      【讨论】:

      • 感谢您的回答,在 context.addPath(fillPath.cgpath) 行我收到以下错误:使用未解析的标识符“fillPath”
      • 我没有收到错误但圆圈没有出现
      • 您是否在 ViewController 中将MyProgress 的实例添加到self.view
      • 不,我该怎么做?
      • 我已经按照你说的添加了视图,但是当我运行代码时,屏幕显示为空白
      猜你喜欢
      • 2015-12-03
      • 2014-12-30
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 2014-04-11
      • 2012-09-19
      • 1970-01-01
      相关资源
      最近更新 更多