How can I animate a radial gradient ?

The question is "How can I animate a radial gradient ?"

I find 2 ways to display a gradient, one is CGGradientRef, the other is CAGradientLayer,

I have problem with both to do what I want , I can draw a radial gradient with CGGradientRef in -drawRect: method, but it can not be animated, while the CAGradientLayer can be animated but it provide only axial gradient, not radial one.

Give your view class a property which represents what you want to animate about the gradient. The property should be of float, double, NSPoint, NSSize, or NSRect type. In the setter of your property, in addition to recording the new value in an instance variable, have the view mark itself as needing display. Be sure to use the property value as appropriate in your -drawRect: method to affect how the gradient draws itself.


Then, override +defaultAnimationForKey:, which NSView gets from the NSAnimatablePropertyContainer protocol, to return an animation for that property. The docs for +defaultAnimationForKey: give an example implementation.


Then, you can do "yourView.animator.yourProperty = newValue" somewhere and the view will animate the change of the property.

does it work with UIColor? because I want to animate the color of my view,like from green to red, it's not a property of float,double,NSPoint,NSSize or NSRect...

I'm not finding documentation that says animating NSColor-typed properties is supported, but give it a try. It seems like it would be an easy thing for AppKit to support.


If it doesn't work and you're working with RGB colors, you can make three properties, red, green, and blue, of type CGFloat. (Or, if it makes more sense for your needs, you can use hue, saturation, and brightness.) You'd create the necessary NSColor from those components as necessary (probably in -drawRect:). You can animate any or all of those properties in a given animation group. A linear, component-wise interpolation may not be a completely colorimetrically correct approach, but it will probably work well enough.

How can I animate a radial gradient ?
 
 
Q