2017-06-15 48 views
1

Dotted line斯威夫特:绘制半透明线与drawStroke(_:触摸:)

我想提请使用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() 

} 
+0

你有任何解决方案,请分享。 – vaibhav

回答

0

如果设置了阿尔法在设置strokeColor或者直接像context?.setAlpha(0.3)这将绘制像上面运算的图像点线路的对象,这是我的解决方法和它的作品完美。

只需获取两个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 
    } 
} 

交付这个答案只是为了更新SO库。