Custom animatable property returns NSNull?

丶灬走出姿态 提交于 2019-12-12 08:53:09

问题


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!


回答1:


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.




回答2:


Try using [UIView animateWithDuration:0.5 animations:^{ }];



来源:https://stackoverflow.com/questions/25016137/custom-animatable-property-returns-nsnull

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!