C# WPF翻译格式
我正在尝试使用TranslateTransform类在Y轴上的网格上移动图像。我需要这个动作是平滑的,所以我不能使用SetMargin或SetCanvas。我在代码隐藏中尝试了这一点:C# WPF翻译格式,c#,wpf,animation,transform,C#,Wpf,Animation,Transform,我正在尝试使用TranslateTransform类在Y轴上的网格上移动图像。我需要这个动作是平滑的,所以我不能使用SetMargin或SetCanvas。我在代码隐藏中尝试了这一点: public void MoveTo(Image target, double oldY, double newY) { var trans = new TranslateTransform(); var anim2 = new DoubleAnimation(0, newY, TimeSpan.
public void MoveTo(Image target, double oldY, double newY)
{
var trans = new TranslateTransform();
var anim2 = new DoubleAnimation(0, newY, TimeSpan.FromSeconds(2))
{EasingFunction = new SineEase()};
target.RenderTransform = trans;
trans.BeginAnimation(TranslateTransform.YProperty, anim2);
}
我要使用的对象(图像控件)放置在网格上。
这是第一次一切正常。
当我尝试使用相同的函数再次移动对象时,问题就出现了。
对象(图像控件)首先移动到开始位置(初始Y坐标),然后动画开始
TranslateTransform是否也可以更改坐标(在我的示例中为边距属性)
谢谢。转换不会更改原始值。它们是您的原点。如果每次移动时都需要新的原点,则可以处理动画完成事件。或者,可以从变换中获取当前偏移,并将其作为动画的新起点
换句话说,开始值将始终是最后一次移动到值。TranslateTransform是一种特定的渲染转换。与更改控件的属性(如边距属性)不同,它只会影响控件在屏幕上的显示方式。您已明确告知动画从0开始。它在做你告诉它的事。 只需从value中删除显式的零
,一切都将正常工作
var anim2 = new DoubleAnimation(newY, TimeSpan.FromSeconds(2))
{ EasingFunction = new SineEase() };
必须使用DoubleAnimation的“按”属性。
试试看:
//everytime you execute this anmation your object will be moved 2.0 further
double offset = 2.0
var anim2 = new DoubleAnimation(newY, TimeSpan.FromSeconds(2));
anim2.To = null;
anim2.By = offset;