所以我试图简单地使用缓动函数来动画一个对象的位置会发生的事情是,在这种情况下X中的一个翻译只是转换为新位置然后y在我想要的持续时间之后被动画化x和y同时获得动画这里是使用
的代码var tg = new TransformGroup();
var translation = new TranslateTransform(target.X - Canvas.GetLeft(sender), target.Y - Canvas.GetTop(sender));
var translationName = "myTranslation" + translation.GetHashCode();
RegisterName(translationName, translation);
tg.Children.Add(translation);
sender.RenderTransform = tg;
var animY = new DoubleAnimation(0, target.Y - Canvas.GetTop(sender), new Duration(new TimeSpan(0, 0, 0, 1, 0)))
{
EasingFunction = new PowerEase { EasingMode = EasingMode.EaseOut }
};
var animX = new DoubleAnimation(0, target.X - Canvas.GetLeft(sender), new Duration(new TimeSpan(0, 0, 0, 1, 0)))
{
EasingFunction = new PowerEase { EasingMode = EasingMode.EaseOut }
};
var s = new Storyboard();
Storyboard.SetTargetName(s, translationName);
Storyboard.SetTargetProperty(s, new PropertyPath("(0)", new DependencyProperty[] { TranslateTransform.YProperty, TranslateTransform.XProperty }));
var storyboardName = "s" + s.GetHashCode();
Resources.Add(storyboardName, s);
s.Children.Add(animX);
s.Children.Add(animY);
s.Completed +=
(sndr, evtArgs) =>
{
Resources.Remove(storyboardName);
UnregisterName(translationName);
};
s.Begin();
答案 0 :(得分:1)
您应该在每个双动画上设置属性路径,而不是故事板。此外,您指定PropertyPath
的方式不正确。您可以传入依赖项属性的现有实例或描述复杂属性路径的字符串。
另一个小问题是您创建并注册要设置动画的元素的名称。您应该考虑是否真的需要这样做。我建议您只使用Storyboard.SetTarget
代替Storyboard.SetTargetName
。
看一下这段代码:
private static void AnimateObject(Rectangle rectangle)
{
var translateTransform = new TranslateTransform();
rectangle.RenderTransform = translateTransform;
var storyboard = new Storyboard();
var xAnimation = new DoubleAnimation(0.0, 100.0, TimeSpan.FromMilliseconds(500))
{
EasingFunction = new PowerEase()
};
Storyboard.SetTarget(xAnimation, rectangle);
Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(Rectangle.RenderTransform).(TranslateTransform.X)"));
var yAnimation = new DoubleAnimation(0.0, 50.0, TimeSpan.FromMilliseconds(500))
{
EasingFunction = new PowerEase()
};
Storyboard.SetTarget(yAnimation, rectangle);
Storyboard.SetTargetProperty(yAnimation, new PropertyPath("(Rectangle.RenderTransform).(TranslateTransform.Y)"));
storyboard.Children.Add(xAnimation);
storyboard.Children.Add(yAnimation);
storyboard.Begin();
}
在其中,我创建了translate转换并将其添加为渲染转换。之后我创建了两个动画并在它们上设置了目标和属性路径。我将它们都添加到故事板并启动它。因为我使用了Storyboard.SetTarget
,所以在动画完成后我不需要清理。
我希望这会有所帮助。如果您有任何问题,请随时询问。