簡體   English   中英

WPF DoubleAnimation 旋轉方向無 storyboard

[英]WPF DoubleAnimation rotation direction without storyboard

我有一個 UserControl,需要根據我在 canvas 中接收到的外部信息(X、Y 和角度,單位為度數)旋轉和平移,我在其中動態地添加用戶控件。

我使用雙動畫和一個 trasnformgroup 來做到這一點。

我遇到的問題是,當對象需要更新其 position 從角度 > 0 到角度 < 360,例如從 5° 旋轉到 355° 時,animation 更喜歡逆時針旋轉而不是順時針旋轉,我需要.

這是代碼的一部分,其中 body 是我添加到 canvas 的 UserControl:

var bodymove = new TranslateTransform();
var bodygrp = new TransformGroup();
bodygrp.Children.Add(bodyrot);
bodygrp.Children.Add(bodymove);
body.RenderTransform = bodygrp;


private void RotateBody(AgvStatus status, Duration duration, double newx, double newy)
{
    // correct bouncing around zero degrees
    if (-lastangle[status.Agv - 1] >= 0 && status.Angle < 360 && status.Angle > 355)
    {
        status.Angle = -lastangle[status.Agv - 1];
    }
    // Set and begin AGV bodyrot
    DoubleAnimation animrot = new DoubleAnimation(lastangle[status.Agv - 1], -status.Angle, duration); 
    bodyrot.BeginAnimation(RotateTransform.AngleProperty, animrot);
    lastangle[status.Agv - 1] = -status.Angle;
}

如果我收到在零附近反彈的角度,我設法糾正,所以控制停止來回旋轉,例如,如果我收到 0 然后 360 然后 0 然后 360 等等......

我無法糾正的是,如果 object 從大於零的 position 開始並到達小於 360 的位置(順時針旋轉),在程序中它逆時針旋轉。

對於這個 animation,我沒有 storyboard。

因此,感謝@GazTheDestroyer 評論,如果旋轉必須從角度<= 360 逆時針旋轉,我設法解決了超過 360 的問題,如果旋轉必須順時針旋轉,角度從 0 度以上開始,則低於 0 角度:

private void RotateBody(AgvStatus status, Duration duration)
{
    // correct rotation if new angle received (status.Angle) is bouncing around 0/360 degrees
    var destAngle = 0d;
    var lastAngle = lastangle[status.Agv - 1];
    if (-lastAngle >= 0 && -lastAngle <= 2 && status.Angle > 358)
    {
        destAngle = 0;
    }
    else if (-lastAngle <= 360 && -lastAngle >= 358 && status.Angle > 0 && status.Angle < 2)
    {
        destAngle = 360;
    }
    else if (-lastAngle >= 0 && -lastAngle <= 15 && status.Angle <= 360 && status.Angle >= 340)
    {
        destAngle = 360 - status.Angle;
    }
    else if (-lastAngle <= 360 && -lastAngle >= 345 && status.Angle >= 0 && status.Angle <= 15)
    {
        destAngle = 360 + status.Angle;
    }
    else
    {
        destAngle = status.Angle;
    }

    var animrot = new DoubleAnimation(lastangle[status.Agv - 1], -destAngle, duration); // Set and begin AGV bodyrot
    bodyrot.BeginAnimation(RotateTransform.AngleProperty, animrot);
    lastangle[status.Agv - 1] = -status.Angle;
}

暫無
暫無

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

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