Animate a custom property using CoreAnimation in M

2019-02-24 23:15发布

问题:

This is essentially the same question as posted here: Animating a custom property in a CALayer over a year ago and hasn't been answered.

Im creating a custom layer and drawing a circle on it. I'd like to be able to animate the radius of the circle (and other properties later). From what i've read, i've set it up like so:

public class CircleLayer : CALayer
{

    //[Export("radius")]
     //public float Radius { get;set; }
    //EDIT: I've now changed the radius field to what is coded below


        public float Radius;

    [Export("radius")]
    public float getRadius()
    {
        return Radius;
    }

    [Export("setRadius:")]
    public void setRadius(float val)
    {
        Radius = val;
    }

    public float Thickness {get;set;}
    public CGColor Color {get;set;}
    public float GlowAmount {get;set;}

    private SizeF GlowOffset {get;set;}

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

        if(key.Equals("radius"))
        {

            return true; 
        }
        else
            return false;

    }



    public CircleLayer ()
    {
        if(GlowAmount == 0.0f)
            GlowAmount = 10f;

        GlowOffset = new SizeF(0f,0f);
        //CALayer.NeedsDisplayForKey("radius");
    }



    public override void DrawInContext (CGContext context)
    {
        base.DrawInContext (context);
        Console.WriteLine("drawing...........");
        PointF centerPoint = new PointF(125,125);//this.Frame.Width/2,this.Frame.Height/2);


        //Outer circle
        context.AddEllipseInRect(new RectangleF(centerPoint.X - Radius,
                                                centerPoint.Y - Radius,
                                                Radius * 2,
                                                Radius * 2));
        //Inner circle
        context.AddEllipseInRect(new RectangleF(centerPoint.X - InnerRadius,
                                                centerPoint.Y - InnerRadius,
                                                InnerRadius * 2,
                                                InnerRadius * 2));

        //Fill in circle
        context.SetFillColor(Color);
        context.SetShadowWithColor(GlowOffset,GlowAmount,GlowColor);
        context.EOFillPath();


    }
}

But it just doesn't work. I never get the radius key reported when NeedsDisplayForKey gets called (and prints them to the console). I can animate standard properties no problem (eg: scale)

Edit: Note that I can now successfully modify the value of the property Radius using SetValueForKey. If I do this I need to call SetNeedsDisplay() to update the screen, however i still cannot get the animation to work at all.

Edit #2: Sample attached: http://dl.dropbox.com/u/8617393/GraphicsTest1.zip

回答1:

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.