2015-04-02 16 views
4

我正试图让这个画线在平底锅手势发生的地方。问题是当我尝试使用此代码使用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. 

回答

14

这是告诉你,你正在尝试调用一个有效的上下文(一CGContextRef)之外CoreGraphics中的功能。您不应该拨打电话号码stroke,除非您在的子类别中或在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 
    } 
} 
+0

谢谢,它的工作原理 – traw1233 2015-04-03 01:44:48

相关问题