Click here to Skip to main content
14,735,239 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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);
           
        }
Posted

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
   
v2
Comments
Ron Beyer 21-Dec-13 22:46pm
   
+5, waiting shouldn't be a word in UI's, think notifications :)
Sergey Alexandrovich Kryukov 21-Dec-13 22:53pm
   
Exactly. Thank you, Ron.
—SA
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);
}


Regards
Joseph Leung
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900