动画化CALayer子类的自定义属性 [英] Animating a custom property of CALayer subclass

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

问题描述

我有一个CALayer子类MyLayer,它有一个名为myInt的NSInteger属性。我真的想通过CABasicAnimation为这个属性设置动画,但似乎CABasicAnimation只适用于所谓的动画属性(边界,位置等)。有什么我可以覆盖以使我的自定义myInt属性可动画吗?

I have a CALayer subclass, MyLayer, that has a NSInteger property called myInt. I'd really like to animate this property via CABasicAnimation, but it seems CABasicAnimation only works on so-called "animatable" properties (bounds, position, etc). Is there something I can override to make my custom myInt property animatable?

推荐答案

是的,它是可能的(仅在最新的Core动画中)我相信,即iPhone 3.0+和OS X 10.6 +版本。

Yes, it's possible (only in the latest Core Animation releases though, I believe, i.e. iPhone 3.0+ and OS X 10.6+).


  1. 让你的财产动态,以便CA实现你的访问者:

  1. Make your property dynamic so that CA implements the accessors for you:

@dynamic myInt;


  • 告诉图层属性的更改需要重绘:

  • Tell the layer that changes of the property require redrawing:

    + (BOOL)needsDisplayForKey:(NSString*)key {
        if ([key isEqualToString:@"myInt"]) {
            return YES;
        } else {
            return [super needsDisplayForKey:key];
        }
    }
    


  • 使用<$ c $的值c> myInt 在 drawInContext:方法中。现在,当您为 myInt 设置动画时,Core Animation将为动画的每个步骤插值,并反复让图层自行绘制。

  • Use the value of myInt in your drawInContext: method. Now, when you animate myInt, Core Animation will interpolate the values for each step of the animation and repeatedly ask the layer to draw itself.

    如果您还想为此属性启用隐式动画,还要覆盖 actionForKey:

    If you also want to enable implicit animations for this property, also override actionForKey:.

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

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