2017-06-15 14 views
1

Dotted lineスウィフト:タッチ:):私は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() 

} 
+0

解決策がありましたら、教えてください。 – vaibhav

答えて

0

は、ここに私の回避策であり、それは完璧に動作します。

2つのimageViewを取得し、2つ目の画像ビュー(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リポ​​ジトリを更新するANS配信。

関連する問題