Good evening. I had a similar problem, but with getting the position of the element while animating its movement. I solved this in my case using time. This approach is based on knowing the elapsed time since the start of the animation. Say we have this piece of element class code:
Location = new Point(100, 100); // set animations var moveTime = 5000.0; var function = new SineEase { EasingMode = EasingMode.EaseInOut }; var shift = new Point(1000, 700); var locationAnimationX = new DoubleAnimation { Duration = TimeSpan.FromMilliseconds(moveTime), To = shift .X, EasingFunction = function }; var locationAnimationY = new DoubleAnimation { Duration = TimeSpan.FromMilliseconds(moveTime), To = shift .Y, EasingFunction = function }; // attach animations to this element and its properties Storyboard.SetTarget(locationAnimationX, this); Storyboard.SetTargetProperty(locationAnimationX, "(UIElement.RenderTransform).(CompositeTransform.TranslateX)"); Storyboard.SetTarget(locationAnimationY, this); Storyboard.SetTargetProperty(locationAnimationY, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)"); // create storyboard and start animation of move var moveStory = new Storyboard(); moveStory.Children.Add(locationAnimationX); moveStory.Children.Add(locationAnimationY); moveStory.Begin(); var startTime = DateTime.Now;
For example, we need to know the current location after a while. So let's get started.
var elapsedTime = DateTime.Now.Subtract(startTime).TotalMilliseconds; var ratio = elapsedTime / moveTime; var funcCurrentValue = function.Ease(ratio); var currentShift = new Point(shift.X * funcCurrentValue, shift.Y * funcCurrentValue);
Finally, the current location is set:
var currentLocation = new Point(Location.X + currentShift.X, Location.Y + currentShift.Y);
source share