自定义UIView中的上下文0x0无效-后续问题

时间:2018-09-13 16:56:38

标签: ios swift uiview subclass

此帖子: invalid context 0x0 within custom UIView ...应该给了我解决问题的钥匙,但是我无法避免无效的上下文错误。一些澄清将不胜感激。

我想在加载ViewController时显示的子视图中绘制线条。为此,我创建了UIView的子类,它类似于StartPlayer发布的SplitCircleView。

要运行代码,请创建一个新项目,并将ViewController文件的内容替换为:

import UIKit
@IBDesignable

class ViewController: UIViewController {
    let mySubViewInstance = SplitCircleView()

    override func viewDidLoad() {
        super.viewDidLoad()

        //let mySubViewInstance = SplitCircleView()
        mySubViewInstance.setNeedsDisplay(mySubViewInstance.frame)
    }
}

class SplitCircleView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        //self.setNeedsDisplay()
        //draw(frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //self.setNeedsDisplay()
        //draw(frame)
    }

    override func setNeedsDisplay(_ rect: CGRect) {
        self.backgroundColor = .clear

        drawSlice(rect: rect, startPercent: 87.5, endPercent: 37.5, color: .green)
        drawSlice(rect: rect, startPercent: 37.5, endPercent: 87.5, color: .red)
    }

    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 * .pi * 2 - .pi
        let endAngle = endPercent / 100 * .pi * 2 - .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()
    }
}

如何在加载ViewController时避免无效的上下文错误并在子视图中绘制内容?

2 个答案:

答案 0 :(得分:2)

您应该覆盖draw,而不是setNeedsDisplay

override func draw(_ rect: CGRect) {
    self.backgroundColor = .clear

    drawSlice(rect: rect, startPercent: 87.5, endPercent: 37.5, color: .green)
    drawSlice(rect: rect, startPercent: 37.5, endPercent: 87.5, color: .red)
}

背景颜色的设置只需进行一次,因此draw并不是该行的理想位置。将其移至init

setNeedsDisplay表示需要更新工程图。但是,draw是真正的绘图。

答案 1 :(得分:0)

有两个要修复的地方。

您正在创建一个新视图,因此需要将其添加到viewController的视图中

 override func viewDidLoad() {
    super.viewDidLoad()

    let mySubViewInstance = SplitCircleView.init(frame: self.view.bounds)
    self.view.addSubview(mySubViewInstance)

  //  mySubViewInstance.setNeedsDisplay(mySubViewInstance.frame)
}

第二个是不使用setNeedsDisplay()绘制函数;

override func draw(_ rect: CGRect) {

       self.backgroundColor = .clear
    drawSlice(rect: rect, startPercent: 87.5, endPercent: 37.5, color: .green)
    drawSlice(rect: rect, startPercent: 37.5, endPercent: 87.5, color: .red)
}

因此,您将在这里得到漂亮的图画。

enter image description here