WPF中具有BeginAnimation的动画序列

时间:2013-01-12 16:20:58

标签: c# wpf animation graphics

我正在尝试使用WPF在3D中旋转一些旋转,如果我手动触发它们(在点击时)一切都很好,但是如果我计算应该在Viewport3D上进行的动作,所有动画似乎都会在同一时间。

计算运动的代码如下:

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 个答案:

答案 0 :(得分: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处理程序中启动。

相关问题