1

rotatingbuttonat click180 degree它的工作正常

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

但是当我以 90 度角用rotating两次做同样的事情时,它就不起作用了

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

它只有rotating一个90 degree角度

有人对这个奇怪的问题有任何想法吗?

更新

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();
        }
4

1 回答 1

1

如果您不提供RotationAnimation方法的代码,就很难知道发生了什么。尽管如此,这种方法很可能试图同时执行两次相同的动画。所以你有两个动画试图将同一个元素从 0 度旋转到 90 度。这使得...... 90度。要么改变当前执行动画的旋转角度,要么等待当前动画完成执行后再启动新动画。要求两个不同的动画同时为同一属性设置动画可能无法提供一致的结果。

编辑:看到你的代码后,这确实是问题所在。您正在创建两个动画,它们都将从 0 度旋转到 90 度。您需要链接两个动画,并为您的方法添加一个“原点”参数,以便第二个动画将从 90°(而不是从 0)开始旋转。要链接动画,您可以修改方法以返回创建的动画,然后订阅Completed事件。

像这样的东西:

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

然后你可以这样称呼它:

var storyboard = RotateAnimation((FrameworkElement)sender, 0, 90, 1, "Y");
storyboard.Completed += (s, e) => RotateAnimation((FrameworkElement)sender, 90, 90, 1, "Y");
于 2013-06-10T07:32:22.943 回答