I was almost on the point of asking about this, but I found a solution, and I 
wanted to put it into Google in case anyone else runs into it.

I had a CAShapeLayer that I had simply filled with a flat color. I decided to 
try for a gradient fill because Everything is Better with Gradients. 
CAShapeLayer doesn't do gradients, so I had to combine it with a masked 
CAGradientLayer.

My first attempt looked like this

* Create a CALayer subclass to contain the shape and the gradient.

* Create a CAShapeLayer (shapeL) with my original shape.
        - Fill it with clearColor so the gradient would show through.
        - Stroke it with blackColor.

* Create the gradient layer (gradL).
        - Set startPoint, endPoint, frame, colors, etc.

* Create a duplicate CAShapeLayer (maskL) with [[CAShapeLayer alloc] 
initWithLayer: shapeL].
        - Adjust the fill color to blackColor, because masking goes by alpha.

* Set gradL.mask to maskL, so the gradient will be clipped to the shape.

* Add gradL and shapeL to self.
        - Add a shadow to self because Everything is Also Better with Shadows.

The container layer drew only shapeL. Nothing I could do with the mask, the 
gradient, or the shape did any good. However, not setting gradL.mask at all did 
draw the gradient (as a rectangle).

The trick was that -initWithLayer: did not produce a usable mask layer. If I 
created maskL as a new mask layer ([CAShapeLayer layer]), and initialized it to 
match shapeL (except for the fill), gradL was drawn as expected.

<http://stackoverflow.com/questions/4733966/applying-a-gradient-to-cashapelayer>
 got me most of the way, but didn't extend to how to re-use the mask layer.

        — F


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to