I have a custom CALayer within which I'm trying to enable the animation of certain properties using actionForKey and following this tutorial.
I have a CGFloat
property that will change perfectly when inside an animation block but my other property, a UIColor
, will not.
Here's my function:
- (id<CAAction>)actionForKey:(NSString *)event {
if ([self presentationLayer] != nil && [[self class] isCustomAnimationKey:event]) {
id animation = [super actionForKey:@"backgroundColor"];
if (animation == nil || [animation isEqual:[NSNull null]]) {
[self setNeedsDisplay];
return [NSNull null];
}
[animation setKeyPath:event];
[animation setFromValue:[self.presentationLayer valueForKey:event]];
[animation setToValue:nil];
return animation;
}
return [super actionForKey:event];
}
The colour is being set using CGContextSetFillColorWithColor
but I can see from logs that the colour simply changes from one to the next without any of the interpolated values.
Any ideas?
This is my code and not an answer. There are three classes: OCLayer, OCView and OCViewController. You can see the value of "percent" is changing during animation while the value of "myColor" won't.
It turned out to be obvious in the end, I needed to expose a
CGColor
property in my CALayer and animate that instead.Edit:
Here's some code for this, using the UIViewCustomPropertyAnimation project as a basis.
In
OCLayer.h
add a new property:In
OCLayer.m
add the @dynamic directive:And update
isCustomAnimKey
:In
OCView.h
add the same property but as aUIColor
. This already existed in my project so didn't require modification, which is great because it didn't break any code.The main changes would be in
OCView.m
as the getter and setter need to convert fromCGColor
toUIColor
and back again.The animation can now be carried out as normal: