2011-07-30 20 views
2

Im试图使用TranslateTransform类在Y轴上的网格上移动图像。我需要这个动作顺利,所以我不能使用SetMargin或SetCanvas。我试着在后面的代码中:WPF TranslateTransform

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而改变坐标(在我的情况下是Margin属性)吗?

谢谢。

回答

1

转换不会改变原始值。它们是您的原点。如果您每次移动都需要新的起点,则可以处理动画完成事件。或者从变换中获得当前的偏移量,并为动画创建新的起点。

换句话说,你的初始值将永远是你的最后一步将值

0

TranslateTransform是一种特定的渲染改造。而是改变控件的属性(例如保证金属性),它只会影响控件在屏幕上的显示方式。

0

您明确告诉动画从0开始。它按照您所说的去做。 只要删除明确的零fromvalue,一切都会工作。

var anim2 = new DoubleAnimation(newY, TimeSpan.FromSeconds(2)) 
       { EasingFunction = new SineEase() }; 
0

您必须使用DoubleAnimation的By属性。 试试看:

//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; 
相关问题