title: iOS - Circle shaped gradient
body: <p>I am trying to draw a circular shaped gradient.</p>
  
  <pre><code>let backgroundView:UIView = UIView()
  let backgroundLayer:CAShapeLayer = CAShapeLayer()
  let gradient:CAGradientLayer = CAGradientLayer()
  ...
  
  backgroundLayer.frame = CGRectMake(0, 0, backgroundDiameter, backgroundDiameter)
  backgroundLayer.backgroundColor = UIColor.clearColor().CGColor
  backgroundLayer.strokeColor = backgroundStrokeColor
  backgroundLayer.fillColor = backgroundFillColor
  
  
  gradient.colors = [UIColor(red: 0.5, green: 0.5, blue: 0.9, alpha: 1.0).CGColor,  
  UIColor(red: 0.9, green: 0.9, blue: 0.3, alpha: 1.0).CGColor]
  gradient.locations = [0.01, 0.8]
  gradient.frame = backgroundLayer.frame
  
  backgroundView.frame = CGRectMake(0, 0, backgroundDiameter, backgroundDiameter)
  backgroundView.backgroundColor = UIColor.clearColor()
  backgroundView.center = ringControlCenter
  backgroundLayer.insertSublayer(gradient, atIndex: 1)
  backgroundLayer.path = CGPathCreateWithEllipseInRect(backgroundLayer.frame, nil)
  
  backgroundView.layer.addSublayer(backgroundLayer)
  self.addSubview(backgroundView)
  </code></pre>
  
  <p>However the gradient does not seem to be impacted by:</p>
  
  <pre><code>backgroundLayer.path = CGPathCreateWithEllipseInRect(backgroundLayer.frame, nil)
  </code></pre>
  
  <p>And still have its initial shape.
  Is there a way to mask the gradient with an ellipse shaped layer without using CGContext* instructions?</p>
  
  <p>Thank you,</p>
  
  <p>MG</p>