1

我正在尝试使用 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);

axis和的计算angle不是耗时的简单属性,所以我猜问题是所有动画都会触发下一帧,因为当当前帧“结束”时计算已经完成。

如何在代码(不是 XAML)中按顺序显示这些动画,而不是一次全部显示?

PS:一切都在 C# 中,没有 XAML。

4

1 回答 1

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处理程序中启动。

于 2013-01-12T16:55:25.750 回答