2013-01-12 52 views
0

我試圖用WPF動畫3D中的一些旋轉,如果我手動觸發它們(點擊)一切都很好,但如果我計算應該在Viewport3D上所做的動作動畫似乎在同一時間熄滅。WPF中的動畫序列與BeginAnimation

,計算運動的代碼如下:

for(int i=0; i<40; i++){ 
    foo(i); 
} 

foo(int i)樣子:

//compute axis, angle 
AxisAngleRotation3D rotation = new AxisAngleRotation3D(axis, angle); 
RotateTransform3D transform = new RotateTransform3D(rotation, new Point3D(0, 0, 0)); 
DoubleAnimation animation = new DoubleAnimation(0, angle, TimeSpan.FromMilliseconds(370)); 

rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation); 

axisangle計算是不是費時,簡單的歸因,所以我想問題是所有的動畫都會觸發下一幀,因爲當前幀已經「結束」時已經完成了計算。

如何在代碼(而不是XAML)中順序顯示這些動畫,而不是一次顯示這些動畫? PS:所有內容都在C#中,不支持XAML。

回答

1

您可以添加多個動畫到Storyboard並設置每個動畫的BeginTime以前的動畫的持續時間的總和:

var storyboard = new Storyboard(); 
var totalDuration = TimeSpan.Zero; 

for (...) 
{ 
    var rotation = new AxisAngleRotation3D(axis, angle); 
    var transform = new RotateTransform3D(rotation, new Point3D(0, 0, 0)); 
    var duration = TimeSpan.FromMilliseconds(370); 
    var animation = new DoubleAnimation(0, angle, duration); 

    animation.BeginTime = totalDuration; 
    totalDuration += duration; 

    Storyboard.SetTarget(animation, rotation); 
    Storyboard.SetTargetProperty(animation, new PropertyPath(AxisAngleRotation3D.AngleProperty)); 

    storyboard.Children.Add(animation); 
} 

storyboard.Begin(); 

請注意,我沒有測試上面的代碼,對任何難過故障。


或者你在每一個動畫(從第二個開始)在Completed處理前一個啓動的方式創建動畫。

+0

我不得不說它好一點了,但動畫仍然以大塊的形式出現,我的意思是同時觸發一些觸發器。 – Paul

+1

那麼在之前的Completed處理程序中開始每一個呢? – Clemens

+0

這樣做。謝謝! – Paul