I have a CALayer subclass, MyLayer, that has a NSInteger property called myInt. I'd really like to animate this property via CABasicAnimation, but it seems CABasicAnimation only works on so-called "animatable" properties (bounds, position, etc). Is there something I can override to make my custom myInt property animatable?
Yes, it's possible (only in the latest Core Animation releases though, I believe, i.e. iPhone 3.0+ and OS X 10.6+).
Make your property dynamic so that CA implements the accessors for you:
@dynamic myInt;
Tell the layer that changes of the property require redrawing:
+ (BOOL)needsDisplayForKey:(NSString*)key { if ([key isEqualToString:@"myInt"]) { return YES; } else { return [super needsDisplayForKey:key]; } }
Use the value of myInt
in your drawInContext:
method. Now, when you animate myInt
, Core Animation will interpolate the values for each step of the animation and repeatedly ask the layer to draw itself.
If you also want to enable implicit animations for this property, also override actionForKey:
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With