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