2017-06-15 74 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庫。