title: How do a gradient path that setting the colors from path begin to end
body: <p>I want to draw a gradient path that <strong>from a color in the begin and end in another color</strong>. </p>
  
  <p>Here are two ways to draw gradient line but the effect neither I want. </p>
  
  <p>Someone has any idea to do this?</p>
  
  <pre><code>guard let c = UIGraphicsGetCurrentContext() else {
      fatalError("Current Context NOT Founds.")
  }
  
  c.setLineWidth(width)
  c.setLineCap(cap)
  c.setLineJoin(join)
  
  // the way no.1
  c.saveGState()
  c.move(to: CGPoint(x: 50, y: 50))
  c.addLine(to: CGPoint(x: 50, y: 200))
  c.addLine(to: CGPoint(x: 150, y: 200))
  let alphas: [CGFloat] = [0, 1]
  let colors = [UIColor(white: 1, alpha: alphas[0]).cgColor, UIColor(white: 1, alpha: alphas[1]).cgColor] as CFArray
  let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0.0, 1.0])
  c.addRect(.infinite)
  c.replacePathWithStrokedPath()
  c.clip()
  c.drawLinearGradient(gradient!, start: CGPoint(x: 0, y: 0), end: CGPoint(x: rect.width, y: rect.height), options: CGGradientDrawingOptions(rawValue: 0))
  c.restoreGState()
  
  // the way no.2
  c.saveGState()
  c.beginTransparencyLayer(in: rect, auxiliaryInfo: nil)
  c.move(to: CGPoint(x: 50, y: 250))
  c.addLine(to: CGPoint(x: 50, y: 450))
  c.addLine(to: CGPoint(x: 130, y: 350))
  c.setStrokeColor(UIColor.white.cgColor)
  c.strokePath()
  c.setBlendMode(.sourceIn)
  c.drawLinearGradient(gradient!, start: CGPoint(x: 50, y: 220), end: CGPoint(x: 50, y: 450), options: CGGradientDrawingOptions(rawValue: 0))
  c.endTransparencyLayer()
  c.restoreGState()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/tKcba.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tKcba.png" alt="sample"></a></p>
  
  <hr>
  
  <p>last update 2017/05/26</p>
  
  <p>Here are some sample images that the effect is similar to I want.</p>
  
  <p><a href="https://i.stack.imgur.com/MhjUW.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MhjUW.jpg" alt="sample1"></a></p>
  
  <p><a href="https://i.stack.imgur.com/3q9sr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3q9sr.jpg" alt="sample2"></a></p>
  
  <p><a href="https://i.stack.imgur.com/MerGU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MerGU.jpg" alt="sample3"></a></p>