title: Multiple Drop Shadows on Single View iOS
body: <p>I'm looking to add multiple drop shadows with different opacities to a view. The specs for the shadows are as follows:</p>
  
  <ul>
  <li>Y-offset of 4 with blur radius of 1</li>
  <li>Y-offset of 10 with blur radius of 10</li>
  <li>Y-offset of 2 with blur radius of 4</li>
  <li>Blur radius of 1, spread of 1 (no offsets, will probably have to be 4 different shadows)</li>
  </ul>
  
  <p>I can get all this working just fine using <code>CALayer</code>s. Here's the code I have working for that (please note that I haven't bothered to set <code>shadowPath</code> yet, and won't until I get the multiple shadows thing working):
  </p>
  
  <pre><code>layer.cornerRadius = 4
  layer.masksToBounds = false
  layer.shouldRasterize = true
  let layer2 = CALayer(layer: layer), layer3 = CALayer(layer: layer), layer4 = CALayer(layer: layer)
  layer.shadowOffset = CGSizeMake(0, 4)
  layer.shadowRadius = 1
  layer2.shadowOffset = CGSizeMake(0, 10)
  layer2.shadowRadius = 10
  layer2.shadowColor = UIColor.blackColor().CGColor
  layer2.shouldRasterize = true //Evidently not copied during initialization from self.layer
  layer3.shadowOffset = CGSizeMake(0, 2)
  layer3.shadowRadius = 4
  layer3.shouldRasterize = true
  layer4.shadowOffset = CGSizeMake(0, 1)
  layer4.shadowRadius = 1
  layer4.shadowOpacity = 0.1
  layer4.shouldRasterize = true
  layer.addSublayer(layer2)
  layer.addSublayer(layer3)
  layer.addSublayer(layer4)
  </code></pre>
  
  <p>(While this code is in Swift, I trust that it looks familiar enough to most Cocoa/Objective-C developers for it to make sense. Just know that <code>layer</code> is equivalent to <code>self.layer</code> in this context.)</p>
  
  <p>The problem, however, arises when I attempt to use different opacities for each shadow. The <code>shadowOpacity</code> property of <code>layer</code> ends up being applied to all of its sublayers. This is a problem, as I need all of them to have their own shadow opacity. I have tried setting each layer's shadow opacity to its correct value (<code>0.04</code>, <code>0.12</code>, etc.), but then the opacity of <code>0.04</code> of <code>layer</code> is applied to all sublayers. So I tried to set <code>layer.shadowOpacity</code> to <code>1.0</code>, but this made all the shadows solid black. I also tried to be clever and do <code>layer2.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.12).CGColor</code>, but it was just changed to full black with no transparency.</p>
  
  <p>I suppose it makes some sort of sense that the layers should all have the same shadow opacity. But what's a way to get this working, varying opacities and all (doesn't have to utilize <code>CALayer</code> if it's easier another way)?</p>
  
  <p><strong>Please don't answer with "just use an image"</strong>: no matter how sane that may be, I'm trying to avoid it. Just humor me.</p>
  
  <p>Thanks.</p>
  
  <p>EDIT: As per request, here's what I'm after: <img src="https://i.stack.imgur.com/vJ4AC.png" alt="Goal">.</p>