1
スウィフト:タッチ:):私はdrawStroke(_を使用して半透明の線を描画しようとしていますタッチ:)
:drawStroke(_と半透明の線を引きます。私はコンテキストのアルファ値を変更しましたが、軽いブラシの代わりに点線が付きました。私は何かがタッチ操作で間違っていると仮定します。それを避ける方法はありますか?これは、上記オペアンプの画像のようなドットで線を描画しますcontext?.setAlpha(0.3)
のようにオブジェクトに直接strokeColor
を設定したりしながら、あなたがアルファを設定した場合
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