0

I'm trying to animate an image to move to a certain point then turn and come back to the startign point,I searched this site for answers but i still can't seem to understand how the RenderTransform works.

This is my current code for anyone interested in helping me:

public static void MoveTo(this Image target, double newX, double newY,int s)
    {
        Vector offset = VisualTreeHelper.GetOffset(target);
        DoubleAnimation animay = new DoubleAnimation(0, newY - offset.Y, TimeSpan.FromSeconds(s));
        DoubleAnimation animax = new DoubleAnimation(0, newX - offset.X, TimeSpan.FromSeconds(s));
        TransformGroup transformGroup = new TransformGroup();
        TranslateTransform translatetransform = new TranslateTransform();
        ScaleTransform scaletransform = new ScaleTransform();
        transformGroup.Children.Add(translatetransform);
        transformGroup.Children.Add(scaletransform);
        target.RenderTransform = transformGroup;
        target.RenderTransform.BeginAnimation(TranslateTransform.YProperty, animay);
        target.RenderTransform.BeginAnimation(TranslateTransform.XProperty, animax);
    }

Edit: Just to be clear I should add that I know how to animate the object to move and flip separately,but can't make them work in succession with a Transform Group.

Wazowsky
  • 1
  • 2
  • Your code looks very similar to http://stackoverflow.com/questions/4214155/wpf-easiest-way-to-move-image-to-x-y-programmatically and those answers appear valid. Have not tried. Did you see this page? – Kory Gill Mar 07 '16 at 16:39
  • You are trying to animate the `TranslateTransform.XProperty` and `YProperty` of a TransformGroup, which doesn't have these properties. Why do you have a TransformGroup at all when you only want to move the Image? Get rid of the ScaleTransform and the TransformGroup, and directly assign the TranslateTransform to `target.RenderTransform`. Then the animations should work. It is however questionable if you really need to get and use the `offset` value. – Clemens Mar 07 '16 at 16:48
  • @Clemens as I said in my original post I want to animate the turn that's why I used ScaleTransform and a group. – Wazowsky Mar 07 '16 at 16:50
  • If you have trouble understanding the RenderTransform property, you might as well put the Image in a Canvas and animate its `Canvas.Left` and `Canvas.Top` properties. – Clemens Mar 07 '16 at 16:50
  • No idea how a "turn" would be animated by scaling. You should probably add some details to your question about how exactly the animation should look like. – Clemens Mar 07 '16 at 16:51
  • If you animate the ScaleX property of ScaleTransform to -1 you flip it the other way.I know how to animate the properties separately but not how to put them together to work in succession.@Clemens – Wazowsky Mar 07 '16 at 16:54
  • Call `translatetransform.BeginAnimation` instead of `target.RenderTransform.BeginAnimation`. – Clemens Mar 07 '16 at 17:06
  • @Clemens it still doesn't work like I want it to,calling `translatetransform.BeginAnimation` and then `scaletransform.BeginAnimation` doesn't execute the transforms in the right order – Wazowsky Mar 07 '16 at 17:50

0 Answers0