c# - 没有情节提要的WPF DoubleAnimation旋转方向
问题描述
我有一个 UserControl,它需要根据我在画布内收到的外部信息(X、Y 和角度)旋转和平移,我在其中动态地添加用户控件。
我使用双动画和一个 trasnformgroup 来做到这一点。
我遇到的问题是,当对象需要将其位置从角度 > 0 更新到角度 < 360,例如从 5° 旋转到 355° 时,动画更喜欢逆时针旋转而不是顺时针旋转,我需要.
这是代码的一部分,其中 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 的位置(它顺时针旋转),在程序中它会逆时针旋转。
我没有这个动画的故事板。
解决方案
因此,感谢@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;
}
推荐阅读
- xcode - 将 Pod 提交到 GitHub 存储库
- javascript - 我想返回数组中最长镜像子部分的长度
- php - Zip 文件夹并在 Joomla 后端下载
- angular - Angular 10反应形式重置角度材料日期选择器不起作用
- java - 带有键值集合的映射
- django - 如何在 django-restfmework 的序列化程序中显示子类别
- r - R - 我应该如何在 timevis 中格式化 BCE 日期?
- mongodb - MongoDB:查询集合并按另一个集合中的数组过滤
- reactjs - Flutter 中类似 React 的条件渲染
- android - 问题将 Nativescript angular 升级到 angular 9:在 android 上运行