WPF: Animation Detection or Timeline.Completed Event Cancellation? How?

I move my 3D camera like this:

    Point3DAnimation pa;

    // Triggered by user click
    void MoveCamera(object sender, EventArgs e)
    {

        pa = new Point3DAnimation(myPoint3D, TimeSpan.FromMilliseconds(2000));
        pa.Completed += new EventHandler(pa_Completed);
        Camera.BeginAnimation(PerspectiveCamera.PositionProperty, pa); // anim#1
    }

    // we're in place. do some idle animation
    void pa_Completed(object sender, EventArgs e)
    {
        pa = new Point3DAnimation(myPoint3Ddz, TimeSpan.FromMilliseconds(5000));
        Camera.BeginAnimation(PerspectiveCamera.PositionProperty, pa); // anim#2
    }

      

  • The user clicks.
  • The camera moves to the selected position (animation # 1).
  • When animation # 1 ends, animation # 2 plays.

It's okay ... until the user starts MoveCamera when the previous animation # 1 is not finished.

In this case:

  • A new animation number 1 is launched.
  • Old animator # 1 completed event fired.
  • animation # 2 starts instatntly (overlapping new animation # 1).

2 and 3 are wrong here. How can I avoid this?

I think pa_Completed () should detect that the new animation # 1 is already playing, or MoveCamera () should unregister. Complete the event from the old animator # 1. But what's the correct way to do this?

+1


a source to share


1 answer


If the goal is to combine two animations together, let WPF do the heavy lifting with a class Point3DAnimationUsingKeyFrames

.

First create a keyframe animation in XAML (it's a bear to do this in code):

  <Window.Resources>
    <Point3DAnimationUsingKeyFrames x:Key="CameraMoveAnimation" Duration="0:0:7">
      <LinearPoint3DKeyFrame KeyTime="28%" />
      <LinearPoint3DKeyFrame KeyTime="100%" />
    </Point3DAnimationUsingKeyFrames>
  </Window.Resources>

      



Then destroy it and set the actual Point3D values ​​(using your codenames):

private void MoveCamera(object sender, EventArgs e) {
    Point3DAnimationUsingKeyFrames cameraAnimation = 
        (Point3DAnimationUsingKeyFrames)Resources["CameraMoveAnimation"];
    cameraAnimation.KeyFrames[0].Value = myPoint3D;
    cameraAnimation.KeyFrames[1].Value = myPoint3dz;
    Camera.BeginAnimation(PerspectiveCamera.PositionProperty, cameraAnimation);
}

      

+1


a source







All Articles