自定义动画属性返回NSNull? [英] Custom animatable property returns NSNull?

查看:95
本文介绍了自定义动画属性返回NSNull?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

赏金只剩下一天了!

你能否使用 UIView 自定义动画属性? animateWithDuration块语法?

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

我一直在为自定义 CALayer

我已经设置了自定义 UIView 的支持层,可以使用 CATransaction 为自定义属性设置动画(在自定义示例中,我使用自定义属性更改圆圈的颜色 circleColor )。

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).

我知道 UIView 通过返回 NSNull 来关闭可动画属性 UIView 上的 actionForLayer:forKey:selector 方法。然后当属性包含在 UIView animateWithDuration 时, UIView 将返回动画

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.

所以这适用于 backgroundColor ,不透明度等。但是,我的自定义属性 circleColor 仍然返回 NSNull

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!

推荐答案

所以这个问题已经开放了很长时间,我得出结论认为这是不可能的。下面的代码显示了如何打开要在块中检测到的自定义属性。在UIView中重写此方法。

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;
}

代码允许你在UIView块内制作动画但是没有办法它检测它是否在UIView块内。因此,如果你使用这个代码,那么它甚至会在UIView块之外制作动画,这会破坏目的。我会监视其他人的回复,但看起来唯一的选择就是制作你自己的自定义UIView动画块系统,它允许你添加自定义的动画属性。我认为Facebook的Pop框架将是一个很好的替代解决方案。

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.

这篇关于自定义动画属性返回NSNull?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆