2014-04-01 49 views
2

我已經創建了用於在X中移動對象的動畫,但是如何添加Y?在X和Y中移動對象動畫

TranslateTransform trans = new TranslateTransform(); 
Pointer.RenderTransform = trans; 
DoubleAnimation anim2 = new DoubleAnimation(1, 500, TimeSpan.FromMilliseconds(325)); 
anim2.EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut }; 
anim2.Completed += new EventHandler(myanim_Completed); 
trans.BeginAnimation(TranslateTransform.XProperty, anim2); 
+2

把動畫中的故事板,而不是手動啓動它們。 –

+1

您是否設置了在代碼中執行此任務,或者XAML會執行此任務嗎?我問,因爲這是我做的唯一方法。 –

+0

只有在代碼中,對不起... – Dim

回答

2

使用StoryBoard並添加動畫都爲孩子:

Storyboard storyBoard = new Storyboard 
        { Duration = new Duration(TimeSpan.FromMilliseconds(325)) }; 

DoubleAnimation anim2 = new DoubleAnimation(1, 500, TimeSpan.FromMilliseconds(325)); 
anim2.EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut }; 
anim2.Completed += new EventHandler(myanim_Completed); 
Storyboard.SetTarget(anim2, trans); 
Storyboard.SetTargetProperty(anim2, new PropertyPath(TranslateTransform.XProperty)); 

DoubleAnimation anim1 = new DoubleAnimation(1, 500, TimeSpan.FromMilliseconds(325)); 
anim1.EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut }; 
anim1.Completed += new EventHandler(myanim_Completed); 
Storyboard.SetTarget(anim1, trans); 
Storyboard.SetTargetProperty(anim1, new PropertyPath(TranslateTransform.YProperty)); 

storyBoard.Children.Add(anim2); 
storyBoard.Children.Add(anim1); 

storyBoard.Begin(); 
0

想通了一個答案:

 TranslateTransform trans = new TranslateTransform(); 
     Pointer.RenderTransform = trans; 
     DoubleAnimation animX = new DoubleAnimation(0, 750, TimeSpan.FromMilliseconds(325)); 
     DoubleAnimation animY = new DoubleAnimation(0, 100, TimeSpan.FromMilliseconds(325)); 

     animX.EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut }; 
     animY.EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut }; 

     animX.Completed += new EventHandler(myanim_Completed); 
     trans.BeginAnimation(TranslateTransform.XProperty, animX); 
     trans.BeginAnimation(TranslateTransform.YProperty, animY);