• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Clipping a CAGradientLayer with a duplicated CAShapeLayer (don't)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Clipping a CAGradientLayer with a duplicated CAShapeLayer (don't)


  • Subject: Clipping a CAGradientLayer with a duplicated CAShapeLayer (don't)
  • From: Fritz Anderson <email@hidden>
  • Date: Thu, 09 Feb 2012 13:09:05 -0600

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@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Clipping a CAGradientLayer with a duplicated CAShapeLayer (don't)
      • From: David Duncan <email@hidden>
    • Re: Clipping a CAGradientLayer with a duplicated CAShapeLayer (don't)
      • From: Kyle Sluder <email@hidden>
  • Prev by Date: Re: How to cancel a loading document in NSDocument's readFromURL:ofType:error method ?
  • Next by Date: Re: Clipping a CAGradientLayer with a duplicated CAShapeLayer (don't)
  • Previous by thread: Re: How to cancel a loading document in NSDocument's readFromURL:ofType:error method ?
  • Next by thread: Re: Clipping a CAGradientLayer with a duplicated CAShapeLayer (don't)
  • Index(es):
    • Date
    • Thread