我学习了一篇关于旋转绑定到按钮上的图片的教程,它应该非常简单,但我还是搞不清楚。
private void rotateMenu()
{
int rotateAngle;
if (menuState) rotateAngle = -90;
else rotateAngle = 90;
DoubleAnimation myanimation = new DoubleAnimation(0, rotateAngle, new Duration(TimeSpan.FromMilliseconds(222)));
var rotateTransform = new RotateTransform(rotateAngle, 24.5, 24.5);
menuButtonImage.RenderTransform = rotateTransform;
rotateTransform.BeginAnimation(RotateTransform.AngleProperty, myanimation);
}第一个按钮按下-旋转图片90度。简单有效。
现在,我想要么扭转旋转,要么再旋转90度。动画工作良好,但无论我将第二个rotateAngle设置为什么,结果总是切换到旋转90度的图片。
基本上,我得到的是在图片的第一部分,想要我需要在第二部分。

我在这里做错什么了?为什么我不能再旋转一次?我尝试了- 90,90和很多其他的数值,例如45度,但没有旋转发生?
发布于 2018-11-13 14:42:01
这是因为图片实际上不是旋转的,而是只呈现旋转的。直到渲染转换得到一个新的转换,它将保持旋转。
这将使其向后旋转,因此您可以尝试:
DoubleAnimation myanimation2 =
new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(222)));只使用toValue参数重载将其旋转回来。
我会做这样的事情:
// initial angle is 0
RotateTransform _rotateTransform = new RotateTransform(0.0, 24.5, 24.5);
Duration _rotationSpeed;
private void CreateRotation()
{
_rotationSpeed = new Duration(TimeSpan.FromMilliseconds(222));
menuButtonImage.RenderTransform = _rotateTransform;
}
private void rotateToSide()
{
DoubleAnimation myanimation = new DoubleAnimation(90, _rotationSpeed);
_rotateTransform.BeginAnimation(RotateTransform.AngleProperty, myanimation);
}
private void rotateToDefault()
{
DoubleAnimation myanimation = new DoubleAnimation(0, _rotationSpeed);
_rotateTransform.BeginAnimation(RotateTransform.AngleProperty, myanimation);
}关于您的信息(我可以从您的代码中读取),您不必更改rotateTransform对象的rotateTransform属性。rotateTransform.BeginAnimation将更改该属性。
https://stackoverflow.com/questions/53283372
复制相似问题