Reputation: 3
I followed a tutorial on rotating a picture bound to a button and it should be pretty simple, yet I can't figure it out.
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);
}
First button press - rotate the picture by 90 degrees. Simple, works.
Now I want to either reverse the rotation or rotate another 90 degrees.
Animation works fine but the outcome always switches back to the picture being rotated 90 degrees, no matter what I set the second rotateAngle
to.
Basically what I get is in the first part of the picture, want I need in the second part.
What am I doing wrong here? Why can't I rotate the picture again? I tried with -90, 90 and lots of other values, e.g. 45 degrees, yet no rotation happening?
Upvotes: 0
Views: 549
Reputation: 22073
That's because the picture isn't actually rotated, but it's only rendered rotated. Until the rendertransform gets a new transform, it will stay rotated.
This will rotate it back, So you could try:
DoubleAnimation myanimation2 =
new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(222)));
only use the toValue
parameter overload to rotate it back.
I would make something like:
// 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);
}
For your information (as I can read from your code) You don't have to change the Angle
property of the rotateTransform
object. The rotateTransform.BeginAnimation
will alter the property.
Upvotes: 1