本文介绍了如何等到动画结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行动画功能并等到它完成而不是做其他事情





我该怎么办?





i want to execute animation function and wait until it finish than do something else


how can i do that?


// here is the event function
    private void image2_MouseEnter(object sender, MouseEventArgs e)
        {
  MoveTo(image2, 0, 200);
// wait until the previous  function  finse and start new one
  MoveTo(image2, 0, -200);

        }

// this is the animation function

 static   void MoveTo(  Image target, double newX, double newY)
        {
            var top = Canvas.GetTop(target);
            var left = Canvas.GetLeft(target);
            TranslateTransform trans = new TranslateTransform();
            target.RenderTransform = trans;
            DoubleAnimation anim1 = new DoubleAnimation(top, newY - top, TimeSpan.FromSeconds(2));
            DoubleAnimation anim2 = new DoubleAnimation(left, newX - left, TimeSpan.FromSeconds(2));
            trans.BeginAnimation(TranslateTransform.XProperty, anim1);
            trans.BeginAnimation(TranslateTransform.YProperty, anim2);

        }

推荐答案


static IEnumerable<DoubleAnimation> MoveTo(Control target, double origX, double origY, double newX, double newY)
{

    TranslateTransform trans = new TranslateTransform();
    target.RenderTransform = trans;
    var a = new DoubleAnimation(origX, newY, TimeSpan.FromSeconds(2));
    Storyboard.SetTargetProperty(a, new PropertyPath("(Canvas.Top)"));
    var b = new DoubleAnimation(origY, newX, TimeSpan.FromSeconds(2));
    Storyboard.SetTargetProperty(b, new PropertyPath("(Canvas.Left)"));

    yield return a;
    yield return b;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
    Control c = sender as Control;
    var top = Canvas.GetTop(c);
    var left = Canvas.GetLeft(c);

    Storyboard sb = new Storyboard();
    foreach (var ab in MoveTo(c, left, top, 250, 250))
        sb.Children.Add(ab);

    foreach (var ab in MoveTo(c, 250, 250, 50, 50))
    {
        ab.BeginTime = TimeSpan.FromSeconds(5);
        sb.Children.Add(ab);
    }

    (sender as Control).BeginStoryboard(sb);
}





问候

Joseph Leung



Regards
Joseph Leung


这篇关于如何等到动画结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-02 23:48