我正在尝试使用drawStroke(_:touch :)绘制半透明线。我已经改变了上下文的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()
}
答案 0 :(得分:1)
如果您在设置strokeColor
时设置了alpha,或者直接设置了像context?.setAlpha(0.3)
这样的对象,那么这将使用上面的op图像绘制线条,这是我的解决方法,它完美无缺
只需获取两个imageView并重叠第二个(tempImageView)并在其上绘制图像并在触摸结束时将其传递给main。
// declare
var opacity = 1.0
var isSwiping = false
@IBAction func drawLineWithAlpha(_ sender: UIButton){
opacity = 0.7
}
@IBAction func drawSimpleLine(_ sender: UIButton){
opacity = 1.0
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
isSwiping = true;
drawLine(capMode: .round, touches: touches, blendMode: .normal)
}
func drawLine(capMode: CGLineCap, touches: Set<UITouch>, blendMode: CGBlendMode){
let imageViewTouch:UIImageView = self.tempImageView
if let touch = touches.first{
let currentPoint = touch.location(in: imageViewTouch)
UIGraphicsBeginImageContextWithOptions(imageViewTouch.frame.size, false, 0)
imageViewTouch.image?.draw(in: CGRect(x: 0, y: 0, width: imageViewTouch.frame.size.width, height: imageViewTouch.frame.size.height))
UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: currentPoint.x, y: currentPoint.y))
UIGraphicsGetCurrentContext()?.setLineCap(capMode)
UIGraphicsGetCurrentContext()?.setLineWidth(8.0)
UIGraphicsGetCurrentContext()?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
UIGraphicsGetCurrentContext()?.setBlendMode(blendMode)
UIGraphicsGetCurrentContext()?.strokePath()
imageViewTouch.image = UIGraphicsGetImageFromCurrentImageContext()
imageViewTouch.alpha = opacity
UIGraphicsEndImageContext()
lastPoint = currentPoint
}
}
override func touchesEnded(_ touches: Set<UITouch>,
with event: UIEvent?){
UIGraphicsBeginImageContext(mainImageView.frame.size)
mainImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: .normal, alpha: 1.0)
tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: .normal, alpha: opacity)
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
tempImageView.image = nil
print("touchesEnded")
if(!isSwiping) {
// code for touch end
}
}
发送此ans只是为了更新SO存储库。