title: Swift: Drawing a semi-transparent line with drawStroke(_:touch:)
body: <p><a href="https://i.stack.imgur.com/I3qeF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I3qeF.png" alt="Dotted line"></a></p>
  
  <p>I am trying to draw a semi-transparent line using drawStroke(_:touch:). I have changed the alpha value for the context, but instead of lighter brush got a dotted line. I assume something is wrong with touch handling. Is there a way to avoid that?</p>
  
  <pre><code>override func touchesMoved(_ touches: Set&lt;UITouch&gt;, 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()
  
  }
  </code></pre>