UIBezierPath 出现 "invalid context" 错误

Getting "invalid context" error with UIBezierPath

我试图在平移手势发生的地方画一条线。问题是当我尝试使用此代码时

var firstPoint: CGPoint!

var path: UIBezierPath!

@IBAction func panned(gesture: UIPanGestureRecognizer) {
    switch gesture.state {
    case .Began: firstPoint = gesture.locationOfTouch(0, inView: view)
    case .Ended: gesture.cancelsTouchesInView = true
    case .Changed:
        path.moveToPoint(firstPoint)
        path.addLineToPoint(gesture.translationInView(view))
        path.stroke()
    default: break
    }
}

...我收到多个错误,我不太确定该怎么办。

Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSetLineWidth: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSetLineJoin: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSetLineCap: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSetMiterLimit: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSetFlatness: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextAddPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextDrawPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

这告诉您您正在尝试在有效上下文之外调用 CoreGraphics 函数 (CGContextRef)。你不应该调用 stroke 除非你在 UIView 子类的 drawRect 中或者在 UIGraphicsBeginImageContextWithOptionsUIGraphicsEndImageContext 之间(或类似的东西)。但是,如果您不在图形上下文中,则不能仅 stroke 一条路径。

如果你只是想把这个路径添加到一个UIView,你也可以创建一个CAShapeLayer,并使用这个贝塞尔路径的cgPath,并将结果CAShapeLayer 并将其添加为视图层的子层。例如,在 Swift 3 及更高版本中:

var drawPath: UIBezierPath!
var shapeLayer: CAShapeLayer!

func handlePan(gesture: UIPanGestureRecognizer) {
    let location = gesture.location(in: gesture.view)

    if gesture.state == .began {
        drawPath = UIBezierPath()
        drawPath.move(to: location)

        shapeLayer = CAShapeLayer()
        shapeLayer.strokeColor = UIColor.black.cgColor
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.lineWidth = 4.0
        gesture.view?.layer.addSublayer(shapeLayer)
    } else if gesture.state == .changed {
        drawPath.addLine(to: location)
        shapeLayer.path = drawPath.cgPath
    }
}