Implementing a CAKeyFrameAnimation callback in Mon

2019-06-03 12:05发布

问题:

My code works and I animate, but I'm not sure how to do this:

Animation End Callback for CALayer?

...in MonoTouch.

Here's what i've got:

public partial class MyCustomView: UIView
{
    // Code Code Code

    private void RunAnimation()
    {        
         CAKeyFrameAnimation pathAnimation = new CAKeyFrameAnimation();
         pathAnimation.KeyPath = "position";
         pathAnimation.Path = trail; //A collection of PointFs
         pathAnimation.Duration = playbackSpeed;
         Character.Layer.AddAnimation(pathAnimation,"MoveCharacter");
    }

    //Code Code Code
}

MyCustomView is already inheriting from UIView and I can't inherit from two classes. How would I call a function called "AnimationsComplete()" when this is done animating?

回答1:

I'm not in front of my dev machine, but I think you create a class descending from CAAnimationDelegate, implement the "void AnimationStopped(CAAnimation anim, bool finished) method" and then assign an instance of this class to pathAnimation.Delegate (in your sample).

So, something like this (warning - untested code):

public partial class MyCustomView: UIView
{
    private void RunAnimation()
    {        
        CAKeyFrameAnimation pathAnimation = new CAKeyFrameAnimation();

        // More code.

        pathAnimation.Delegate = new MyCustomViewDelegate();

   }
}

public class MyCustomViewDelegate : CAAnimationDelegate
{
    public void AnimationStopped(CAAnimation anim, bool finished)
    {
        // More code.
    }
}