没有动画的重复旋转 C# Wpf

repeat rotation without animation C# Wpf

我有这个功能,每次用鼠标在模型上双击时,模型都会旋转 15 度。它绕Y轴旋转,每旋转一次相机就会向上移动,所以模型会留在场景中,双击3次后不会消失。

模型需要双击24次才能旋转回到初始位置,所以我想做的是只双击一次,模型会自动旋转360度,但我想要在不使用动画的情况下做到这一点。我尝试在 "SpatialMouse-Doubleclick" 函数中使用 for 循环,但没有成功。 例如,如果我在 for 循环

for (int i = 0; i < 20; i++)

模型将仅在其最终位置显示。

我是否可以像动画一样查看 for 循环的步骤而不是只查看最后一步? 或者有没有其他方法可以使用我的代码而不使用动画来进行旋转?

private void SetViewPosition()
{
    Vector3D vAxis = new Vector3D(0, 1, 0);
    //R= AxisAngleRotation3D
    AxisAngleRotation3D myRotation = new AxisAngleRotation3D(vAxis, -45);
    RotateTransform3D myRotationTransform = new RotateTransform3D(myRotation);
    gCamWC = myRotationTransform.Value;
    gCamWC.M14 = -13;
    gCamWC.M24 = 0;
    gCamWC.M34 = 13;

    Point3D camPosition = new Point3D(gCamWC.M14,gCamWC.M24, gCamWC.M34);
    Vector3D startLookAt = new Vector3D(gCamWC.M11, gCamWC.M21, gCamWC.M31);
    Vector3D startLookUp = new Vector3D(gCamWC.M13, gCamWC.M23, gCamWC.M33);

    DrawingControl.Viewport.SetView(camPosition, startLookAt, startLookUp, 0);

}

private void SpatialControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    Vector3D vAxis = new Vector3D(0, 1, 0);

    AxisAngleRotation3D myRotation = new AxisAngleRotation3D(vAxis, -15);
    RotateTransform3D myRotationTransform = new RotateTransform3D(myRotation);
    Matrix3D doTranslation = new Matrix3D();
    doTranslation.M34 = 4;
    gCamWC.Append(myRotationTransform.Value);
    gCamWC.Append(doTranslation);

    Point3D camPosition = new Point3D(gCamWC.M14, gCamWC.M24, gCamWC.M34);
    Vector3D camLookAt = new Vector3D(gCamWC.M11, gCamWC.M21, gCamWC.M31);
    Vector3D camLookUp = new Vector3D(gCamWC.M13, gCamWC.M23, gCamWC.M33);

    DrawingControl.Viewport.SetView(camPosition, camLookAt, camLookUp,0);
}

解决方案

我将函数 SpatialControl_MouseDoubleClick 重命名为 RotatingModel 并将 "MouseButtonEventArgs" 更改为 "EventArgs".

然后我将以下代码添加到我想要调用 RotatingModel() 的函数中。

//Add Dispatcer Time so the Rotation will be repeated
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += RotatingModel;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);//(h,m,s)
dispatcherTimer.Start();

现在当我调试我的项目时,模型每 1 秒自动旋转 15 度。

如果问题是缺少延迟导致 for 循环完成得太快,一种可能的解决方案是使用 Timer 对象:https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

计时器有一个可以设置为任意毫秒数的间隔,并且它有一个 Elapsed 事件。您可以尝试使用间隔,直到旋转速度合适为止。

注意: "If a System.Timers.Timer is used in a WPF application, it is worth noting that the System.Timers.Timer runs on a different thread then the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer." (来源:https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer(v=vs.110).aspx