Core Animation allows for custom animations by implementing the actionForKey method in your CALayer based class:
- (id<CAAction>)actionForKey:(NSString *)key {
// Custom animations
return [super actionForKey:key];
}
I can then create an animation and return it for the onOrderIn
action (i.e. when the layer is added to another layer). This works just fine. If I do the same for onOrderOut
(i.e. the layer is removed from its superlayer), the returned animation is ignored, and the default animation is applied instead.
My goal is to zoom the layer in (onOrderIn
) and out (onOrderOut
):
- (id<CAAction>)actionForKey:(NSString *)key {
if ([key isEqualToString:@"onOrderIn"] || [key isEqualToString:@"onOrderOut"]) {
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
a.duration = 0.25;
a.removedOnCompletion = NO;
a.fillMode = kCAFillModeBoth;
if ([key isEqualToString:@"onOrderIn"]) {
a.fromValue = [NSNumber numberWithFloat:0.0];
a.toValue = [NSNumber numberWithFloat:1.0];
} else {
a.fromValue = [NSNumber numberWithFloat:1.0];
a.toValue = [NSNumber numberWithFloat:0.0];
}
return a;
}
return [super actionForKey:key];
}
Zooming in works, zooming out does not. Instead the default fade out animation is used.
The code might contain some typos, as I'm typing this on another machine.
Can anyone help?