1
我想提请使用drawStroke(_半透明线:触摸:)。我已经改变了上下文的alpha值,但是更轻的刷子取代了虚线。我认为触摸处理有些问题。有没有办法避免这种情况?
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
let context = UIGraphicsGetCurrentContext()
// Draw previous image into context
image?.draw(in: bounds)
drawStroke(context, touch: touch)
// Update image
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
fileprivate func drawStroke(_ context: CGContext?, touch: UITouch) {
let previousLocation = touch.previousLocation(in: self)
let location = touch.location(in: self)
// Calculate line width for drawing stroke
let lineWidth = lineWidthForDrawing(context, touch: touch)
// Set color
drawColor.setStroke()
//Change Alpha
context?.setAlpha(0.3)
context?.setBlendMode(.darken)
// Configure line
context?.setLineWidth(lineWidth)
context?.setLineCap(.round)
// Set up the points
context?.move(to: CGPoint(x: previousLocation.x, y: previousLocation.y))
context?.addLine(to: CGPoint(x: location.x, y: location.y))
// Draw the stroke
context?.strokePath()
}
你有任何解决方案,请分享。 – vaibhav