AKD
AKD

Reputation: 3966

storyBoard animation rotation issue

i am rotating a button on click at 180 degree, and its working fine

AnimationServices.RotationAnimation((FrameworkElement)sender, 180, 1, "Y"); // working

but when i do the same thing by rotating it two times with 90 degree angle than its not working

AnimationServices.RotationAnimation((FrameworkElement)sender, 90, 1, "Y");
AnimationServices.RotationAnimation((FrameworkElement)sender, 90, 1, "Y");  // not working

its only rotating at one 90 degree angle

does anyone have any idea about this strange issue ?

UpDate

internal static void RotationAnimation(FrameworkElement sender, int Angle, int DurationInMSec, String Axis)
        {
            var storyboard = new Storyboard();

            var easingDoubleKeyFrame1 = new EasingDoubleKeyFrame
            {
                KeyTime = TimeSpan.FromMilliseconds(0),
                Value = 0
            };

            var easingDoubleKeyFrame2 = new EasingDoubleKeyFrame
            {
                KeyTime = TimeSpan.FromMilliseconds(DurationInMSec),
                Value = Angle
            };

            var doubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();

            doubleAnimationUsingKeyFrames.KeyFrames.Add(easingDoubleKeyFrame1);
            doubleAnimationUsingKeyFrames.KeyFrames.Add(easingDoubleKeyFrame2);

            var rotateTransform = new PlaneProjection();

            var button = (FrameworkElement)sender;
            button.Projection = rotateTransform;

            Storyboard.SetTarget(doubleAnimationUsingKeyFrames, rotateTransform);
            Storyboard.SetTargetProperty(doubleAnimationUsingKeyFrames, new PropertyPath("Rotation" + Axis));
            storyboard.Children.Add(doubleAnimationUsingKeyFrames);
            storyboard.Begin();
        }

Upvotes: 1

Views: 543

Answers (1)

Kevin Gosse
Kevin Gosse

Reputation: 39007

It's hard to tell what's going on if you don't provide the code of your RotationAnimation method. Still, it's very likely that this method is trying to execute the same animation two times simultaneously. So you have two animations trying to rotate the same element from 0 to 90 degrees. Which makes... 90 degrees. Either change the rotation angle of the currently executing animation, or wait for the current one to finish executing before launching a new one. Asking two different animations to animate simultaneously the same property cannot possibly provide consistent results.

Edit: After seeing your code, that's indeed the problem. You're creating two animations, that will both rotate from 0 to 90 degrees. You need to chain two animations, and add an "origin" parameter to your method so the second animation will start rotating from 90° (and not from 0). To chain the animation, you can modify your method to return the created animation, then subscribe to the Completed event.

Something like that:

internal static Storyboard RotationAnimation(FrameworkElement sender, int origin, int Angle, int DurationInMSec, String Axis)
{
    var storyboard = new Storyboard();

    var easingDoubleKeyFrame1 = new EasingDoubleKeyFrame
    {
        KeyTime = TimeSpan.FromMilliseconds(0),
        Value = origin
    };

    var easingDoubleKeyFrame2 = new EasingDoubleKeyFrame
    {
        KeyTime = TimeSpan.FromMilliseconds(DurationInMSec),
        Value = Angle + origin
    };

    var doubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();

    doubleAnimationUsingKeyFrames.KeyFrames.Add(easingDoubleKeyFrame1);
    doubleAnimationUsingKeyFrames.KeyFrames.Add(easingDoubleKeyFrame2);

    var rotateTransform = new PlaneProjection();

    var button = (FrameworkElement)sender;
    button.Projection = rotateTransform;

    Storyboard.SetTarget(doubleAnimationUsingKeyFrames, rotateTransform);
    Storyboard.SetTargetProperty(doubleAnimationUsingKeyFrames, new PropertyPath("Rotation" + Axis));
    storyboard.Children.Add(doubleAnimationUsingKeyFrames);
    storyboard.Begin();

    return storyboard;
}

Then you can call it like that:

var storyboard = RotateAnimation((FrameworkElement)sender, 0, 90, 1, "Y");
storyboard.Completed += (s, e) => RotateAnimation((FrameworkElement)sender, 90, 90, 1, "Y");

Upvotes: 1

Related Questions