Question

I am new to iPhone development and am currently toying with recreating a charting tool I developed for Silverlight.

Currently I'm using a gradient to 'fill' a rectangle representing a bar within a chart. Is it possible to animate this gradient so it changes colour when a user touches the bar within the chart.

I have looked through the Core Animation guides provided by Apple but cannot see a property which targets gradients. I suppose I could use a transition to fade between two rects, one of which has my starting gradient and the second with the 'touched' version but this would mean obviously drawing multiple rect objects for each bar with I assume extra performace overheads.

Any ideas?

Was it helpful?

Solution

Yes, indeed you can animated gradients with Core Animation.

The CAGradientLayer class that came out in 3.0 has a nice API for rendering gradients into a layer and animating color and color-stop changes as well.

I did a post on this class a little while back, along with some sample code that's linked at the bottom.

In the sample I animate the gradient by building a CABasicAnimation, but you can implicitly animate the change as well, by just passing a new array of colors to the gradient layer's colors property. Use implicit animations unless you have a reason not to.

Check that out and let me know if you have any questions specific to the UI you're trying to animate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top