views:

250

answers:

1

Hi there,

I'm working on an iPhone app that has some non-rectangular UI elements. Currently, I'm subclassing UIView, and in drawRect I'm using a CGPathRef to draw black border and a color-filled interior.

I'd like to make these items look more like "buttons", though, so I'd like to have some of the same sort of "glass effects" that are used on e.g. the icons for an iPhone app (when you don't set UIPrerenderedIcon to true), or in other buttons.

I hunted around, and found this, which seems to be close to what I need:

http://stackoverflow.com/questions/422066/gradients-on-uiview-and-uilabels-on-iphone

But I'm having difficulty figuring out how to clip the gradient to my shape.

It seems like the mask property on the view would be the right place to go, which seems like it would call for me to create a new CALayer object, with the clipping somehow applied to it.

I'm hoping there's some nice convenience function for doing this, though if I need to write something more complicated, that's OK, too. I'm just having difficulty figuring out how to apply the path as a mask. I'm unsure if I need to create a new drawing context and draw the path into it? And then use CGContextClip?

I think I've got a lot of the right pieces figured out, I'm just having difficulty understanding how to assemble them.

Could someone please point me in the right direction? (I'm happy to read more in the docs, just point me in the right direction, please.)

A: 

You can create a CAShapeLayer and set its path to your CGPathRef. Then set the mask property of a CAGradientLayer to your shapeLayer.

Jon Hull