【问题标题】:WPF DoubleAnimation rotation direction without storyboard没有情节提要的WPF DoubleAnimation旋转方向
【发布时间】:2021-03-18 15:17:48
【问题描述】:

我有一个 UserControl,需要根据我在画布内收到的外部信息(X、Y 和角度,单位为度数)旋转和平移,我在其中动态添加用户控件。

我使用双动画和一个 trasnformgroup 来做到这一点。

我遇到的问题是,当对象需要从 > 0 到角度

这是代码的一部分,其中 body 是我添加到画布的 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 等等...

我无法纠正的是,如果对象从大于零的位置开始并到达小于 360 的位置(顺时针旋转),在程序中它会逆时针旋转。

我没有这个动画的故事板。

【问题讨论】:

  • 您可以指定 > 360 度以确保顺时针旋转。然后在动画之后标准化,还是在新动画开始时标准化?
  • @GazTheDestroyer 感谢您的回复。能不能具体一点,我没明白你的意思。
  • 因此,如果当前旋转为 350°,而您想要旋转 10°,则您的动画范围为 350-370,这意味着旋转将是顺时针方向。动画结束后,或者开始下一个动画时,需要从 10 开始,而不是 370。
  • @GazTheDestroyer 非常感谢,问题解决了!!!!!!

标签: c# wpf


【解决方案1】:

所以,多亏了@GazTheDestroyer 的评论,如果旋转必须从角度

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;
}

【讨论】:

  • 为了便于阅读,请将所有 lastangle[status.Agv - 1] 替换为局部变量 var last = lastangle[status.Agv - 1]。还有-last &lt; 0last &gt; 0
猜你喜欢
  • 2016-05-02
  • 1970-01-01
  • 2014-03-10
  • 2014-01-27
  • 1970-01-01
  • 2013-10-26
  • 2015-03-19
  • 2011-12-09
  • 1970-01-01
相关资源
最近更新 更多