Search code examples
iosobjective-cuiviewuikitcore-animation

Custom animatable property returns NSNull?


Only one day left on the bounty!

Can you have a custom animatable property work with the UIView animateWithDuration block syntax?

I've been working on creating my own implicit animation for a custom animatable property on a custom CALayer.

I have set the backing layer of a custom UIView and can use a CATransaction to animate the custom property (in the custom example I am changing the color of a circle with a custom property circleColor).

I know that UIView turns off animatable properties by returning NSNull from the actionForLayer:forKey: selector method on UIView. Then when the property is wrapped in the UIView animateWithDuration block the UIView will return the animation.

So this will work with backgroundColor, opacity, etc. However, my custom property circleColor still returns NSNull.

I hope I provided enough context to the question. Thanks!


Solution

  • So this question has been open for a long-time and I'm concluding that it isn't possible. The code below shows how I would turn on the custom property to be detected within the block. This method is overridden in UIView.

    - (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event {
        NSLog(@"%s - event: %@", __PRETTY_FUNCTION__, event);
        id<CAAction> action = [super actionForLayer:layer forKey:event];
        if ([event isEqualToString:@"circleColor"] && (nil == action || (id)[NSNull null] == action)) {
            return [CABasicAnimation animationWithKeyPath:event];
        }
        return action;
    }
    

    The code allows you to animate within the UIView block however there is no way for it to detect that it is within a UIView block. So if you use this code then it will even animate outside of the UIView block which defeats the purpose. I'll monitor for other people's responses, but the only options it seems is to make your own custom UIView animation block system which will allow you to add custom animatable properties. I would imagine that Facebook's Pop framework would be a great alternative solution.