Can you animate gradients using Quartz in an iPhone?

人走茶凉 提交于 2019-12-21 20:27:53

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/1819311/can-you-animate-gradients-using-quartz-in-an-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!