在这个“奇怪”的问题上寻求帮助。
我正在使用 PanGesture 允许用户在CAShapeLayer
.
我跟踪路径直到它们结束,然后将路径存储在路径数组中,从CAShapeLayer.path
当我将这些路径重绘到 a 上UIImage
时,这些路径会移向Y-axis
屏幕顶部。
我附上两张图片:
1)路径的绘制。绘制路径
2)重绘路径时弹出的UIImage。如您所见,我沿着网格线绘制,但是,当创建 UIImage 时,该线位于网格线上方。UIImage 上重绘的路径
任何建议表示赞赏。
绘图和渲染的代码如下。
@objc private func drawLine(_ gestureRecognizer: UIPanGestureRecognizer) {
let point = gestureRecognizer.location(in: self)
if point.x < 0 || point.x > self.bounds.width || point.y < 0 || point.y > self.bounds.height {
return
}
switch gestureRecognizer.state {
case .began:
currentLine = UIBezierPath()
currentLine.lineWidth = settings.defaultSpotRadius
currentLine.lineCapStyle = .round
currentLine.lineJoinStyle = .round
currentLine.move(to: point)
break
case .changed:
currentLine.addLine(to: point)
currentLineDrawLayer.path = currentLine.cgPath
break
case .ended:
self.storage.addLine(clear: fowClearMode, path: currentLine, ofWidth: currentLine.lineWidth)
currentLineDrawLayer.path = nil
storedImage = drawPaths()
break
default:
break
}
}
public func drawPaths() -> UIImage? {
if pathsHidden { return nil }
let rect = CGRect(origin: .zero, size: self.imageSize)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
storedImage.draw(in: rect)
for (isClear, line, width) in thePaths {
let blendMode = isClear ? settings.colorClear : settings.colorDark
line.lineWidth = width
line.stroke(with: blendMode, alpha: 1.0)
}
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
2018 年 9 月 25 日星期二
我创建了一个仅包含导入点点滴滴的迷你项目。它在这里可用...迷你项目