如何等到动画结束 [英] how to wait until animation finise

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

问题描述

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





我该怎么办?





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);
           
        }

推荐答案

等待的概念与面向均匀的UI的概念相矛盾。即使UI正在显示正在进行的操作,UI仍应保持响应。动画完成时做其他事情只是意味着处理适当的事件。这一个: http://msdn.microsoft.com/en -us / library / system.windows.media.animation.timeline.completed.aspx [ ^ ]。



-SA
The concept of "waiting" contradicts the concept of even-oriented UI. The UI should remain responsive even when it is showing something in progress. "Do something else" when animation finishes simply means handling appropriate event. This one: http://msdn.microsoft.com/en-us/library/system.windows.media.animation.timeline.completed.aspx[^].

—SA


您可以使用Storyboard链接动画:

You may use Storyboard to chain animations:
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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆