gpt4 book ai didi

swift - 使用 UIBezierPath 获取 "invalid context"错误

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:27 24 4
gpt4 key购买 nike

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

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.

最佳答案

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

如果你只是想将这个路径添加到一个UIView,你也可以创建一个CAShapeLayer,并使用这个贝塞尔曲线的cgPath路径,并获取生成的 CAShapeLayer 并将其添加为 View 层的子层。例如,在 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
}
}

关于swift - 使用 UIBezierPath 获取 "invalid context"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29423060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com