簡體   English   中英

重復旋轉而不使用動畫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/zh-cn/library/system.timers。計時器(v = vs.110).aspx

計時器具有一個可以設置為任意毫秒數的間隔,並且它具有一個經過時間事件。 您可以嘗試使用“間隔”,直到轉速合適為止。

注意:“如果在WPF應用程序中使用System.Timers.Timer,則值得注意的是System.Timers.Timer在不同於用戶界面(UI)線程的線程上運行。為了訪問用戶上的對象接口(UI)線程,有必要使用Invoke或BeginInvoke將操作發布到用戶界面(UI)線程的Dispatcher上。線程,可以在DispatcherTimer上設置Dispatcher和DispatcherPriority。” (來源: https : //msdn.microsoft.com/zh-cn/library/system.windows.threading.dispatchertimer ( v=vs.110 ) .aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM