Animating a custom property in a CALayer

I'm trying to get animation working on a custom property in a CALayer.

But I just just am not able to figure out how to get this working correctly. The key "myCounter" is never sent to NeedsDisplayForKey. Are there some steps I'm missing? Below is the class I'm testing which I add to a layer elsewhere. Has anyone got a custom property to animate using monotouch?

    public class TestProperty : CALayer
    {
    //this line updated based on feedback below**********
        public uint myCounter { [Export ("myCounter")] get; [Export setMyCounter:")]  set; }


    public TestProperty ()
    {
        CABasicAnimation anim = CABasicAnimation.FromKeyPath("myCounter");
        anim.From = NSNumber.FromInt32(1);
        anim.To = NSNumber.FromInt32(10);
        anim.Duration = 1.0f;
        anim.RepeatCount = float.MaxValue;
        anim.AutoReverses = true;
        this.AddAnimation(anim,null);
    }

    [Export ("needsDisplayForKey:")]
    static bool NeedsDisplayForKey (NSString key)
    {
        Console.WriteLine("{0}", key.ToString());

        if(key.Equals("myCounter"))
        {
            return true; //never gets here
        }
        else
            return false;

    }
    }

MonoTouch没有与MonoMac一样的自动KVC注册支持,因此您应该使用:

public uint myCounter { [Export ("myCounter")] get; [Export ("setMyCounter:")] set; }

This has unfortunately been impossible to do with MonoTouch - but we've fixed it for the next beta (5.3.3) which will hopefully be released soon.

Once 5.3.3 has been released you can use this sample: https://github.com/xamarin/monotouch-samples/tree/monotouch-5.4/CustomPropertyAnimation to see how to do it.

链接地址: http://www.djcxy.com/p/74166.html

上一篇: 如何继承CALayer并使用自定义属性?

下一篇: 在CALayer中为自定义属性设置动画