In the CALayer subclass I'm working on, I have a custom property that I want to automatically activate, i.e. if the property is called "myProperty", I need the following code:
[myLayer setMyProperty:newValue]
To make a smooth animation from the current value to "newValue".
Using the override approach actionForKey: and needsDisplayForKey: (see the following code) I was able to make it work very nicely, just to interpolate between the old and the new value.
My problem is that I want to use a slightly different animation duration or path (or something else) depending on the current value and the new property value, and I could not figure out how to get the new value from inside ActionForKey:
Thanks in advance
@interface ERAnimatablePropertyLayer : CALayer { float myProperty; } @property (nonatomic, assign) float myProperty; @end @implementation ERAnimatablePropertyLayer @dynamic myProperty; - (void)drawInContext:(CGContextRef)ctx { ... some custom drawing code based on "myProperty" } - (id <CAAction>)actionForKey:(NSString *)key { if ([key isEqualToString:@"myProperty"]) { CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:key]; theAnimation.fromValue = [[self presentationLayer] valueForKey:key]; ... I want to do something special here, depending on both from and to values... return theAnimation; } return [super actionForKey:key]; } + (BOOL)needsDisplayForKey:(NSString *)key { if ([key isEqualToString:@"myProperty"]) return YES; return [super needsDisplayForKey:key]; } @end
Eyal redler
source share