tra*_*233 7 uibezierpath swift
我试图让这个绘制一条平移手势发生的线.问题是当我尝试使用此代码时
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
}
}
Run Code Online (Sandbox Code Playgroud)
...我得到了多个错误,我不太清楚该怎么做.
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.
Run Code Online (Sandbox Code Playgroud)
Rob*_*Rob 17
这告诉您,您正在尝试在有效上下文之外调用CoreGraphics函数(a CGContextRef).你不应该叫stroke除非你内做drawRect的UIView子类之间或UIGraphicsBeginImageContextWithOptions和UIGraphicsEndImageContext(或等价的东西).但是,stroke如果您不在图形上下文中,则不能只是一条路径.
如果您只想将此路径添加到a UIView,您还可以创建一个CAShapeLayer,并使用cgPath此bezier路径,并获取结果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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2901 次 |
| 最近记录: |