title: UIView Shadow Gradient
body: <p>I have created a custom UIView in my iOS project that has a drop shadow.
  My aim is to apply the same gradient to the shadow as it is on the view's background.</p>
  
  <p>Below is an example of how my current solid colour shadows look.</p>
  
  <p><a href="https://i.stack.imgur.com/g0hCx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/g0hCx.png" alt="Shadow Example"></a>
  This is done through a subclass of UIView with the below code:</p>
  
  <pre><code>override func layoutSubviews() {
      let gradientLayer = layer as! CAGradientLayer
      gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
      gradientLayer.startPoint = CGPoint(x: startPointX, y: startPointY)
      gradientLayer.endPoint = CGPoint(x: endPointX, y: endPointY)
      layer.cornerRadius = cornerRadius
      layer.shadowColor = shadowColor.cgColor
      layer.shadowOffset = CGSize(width: shadowX, height: shadowY)
      layer.shadowRadius = shadowBlur
      layer.shadowOpacity = 1
  
      let inset: CGFloat = bounds.width * 0.05
      layer.shadowPath = UIBezierPath(roundedRect: bounds.insetBy(dx: inset, dy: 0.0), cornerRadius: cornerRadius).cgPath
  }
  </code></pre>
  
  <p>I have been playing around with creating a second gradient layer and masking it to the shadow but have had no luck. Please point me in the right direction!</p>