2011-07-30 26 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; 
相關問題