IPhone SDK: How to Draw a Rounded Rectangle With a Semi-Transparent Gradient and a Drop Shadow

core-animationcore-graphicsgradientquartz-graphicsshadows

I'm experimenting with drawing on the iPhone by manually creating parts of the UI for my application (In this case a graph). Essentially, I want to draw a rectangle with rounded corners, a drop-shadow, and a semi-transparent gradient as the fill of the rectangle. Here is a screen shot of my photoshop mockup:
alt text http://img36.imageshack.us/img36/6097/picture1nvf.png

I've gotten has far as creating a solid rounded rectangle with a drop shadow using a CALayer, but in order to get the shape and drop shadow to show up at all I have to set a background color which does not allow me to make my semi-transparent gradient the only fill of the layer.

I've been working on this for a bit now, and have read the entire Core Animation programming guide but I haven't made any further progress. I feel like the answer is right under my nose.

What am I missing?

P.S.I know I could probably use an image file to accomplish this but I'm trying to do it this way because I want to learn, and someday I want to do some fancier animation stuff with this.

Best Answer

  1. Fill the rounded rectangle with the shadow. Do this fill at 100% opacity—you'll effect the transparency in the next step.
  2. Fill the same rounded rectangle without the shadow, using Destination Out blend mode, at (1.0 - the desired opacity). For example, if you want 40%, do the Destination-Out draw at 60%. Think of it as subtracting that much from the opacity of the image.