【问题标题】:Duration of ScalarKeyFrameAnimation got shorterScalarKeyFrameAnimation 的持续时间变短了
【发布时间】:2020-02-09 20:46:51
【问题描述】:

您好,我尝试使用以下代码创建一个“ScalarKeyFrameAnimation”,在第一次尝试时,我只切换一个开关。动画以“旋转”动画对象配置的方式开始和停止,但是当尝试几次时,我注意到视觉旋转越来越慢,然后停止。就像在 GIF 中一样

我已经在 Page_Loaded 事件上移动了旋转动画的创建。所以它只能创建一次。但是没有任何改变

    private Compositor compositor = Window.Current.Compositor;
    private Visual backvisual;        

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        backvisual = ElementCompositionPreview.GetElementVisual(FanIcon);

        backvisual.Size = new Vector2(100, 100);
        backvisual.CenterPoint = new Vector3(backvisual.Size / 2, 0);

        rotate = compositor.CreateScalarKeyFrameAnimation();
        rotate.InsertKeyFrame(1f, 360, compositor.CreateLinearEasingFunction());
        rotate.Duration = TimeSpan.FromMilliseconds(1000);
        rotate.IterationBehavior = AnimationIterationBehavior.Forever;

    }




    private void ToggleFanSec1_Toggled(object sender, RoutedEventArgs e)
    {
        if (ToggleFanSec1.IsOn == true)
        {
            backvisual.StartAnimation(nameof(Visual.RotationAngleInDegrees), rotate);
        }
        else
        {
            backvisual.StopAnimation(nameof(Visual.RotationAngleInDegrees));
        }
    }[![enter image description here][1]][1]

【问题讨论】:

    标签: c# xaml animation uwp composition


    【解决方案1】:

    你应该添加一个初始状态的关键帧,没有初始状态很难定位,容易混乱。您可以更改如下代码。这意味着随着时间的推移,旋转动画从 0 到 360。

    rotate = compositor.CreateScalarKeyFrameAnimation();
    rotate.InsertKeyFrame(0f, 0);
    rotate.InsertKeyFrame(1f, 360, compositor.CreateLinearEasingFunction());
    rotate.Duration = TimeSpan.FromMilliseconds(4000);
    rotate.IterationBehavior = AnimationIterationBehavior.Forever;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-11
      • 2022-06-29
      • 1970-01-01
      • 2012-11-25
      • 2016-06-10
      • 2012-10-09
      • 2020-09-24
      • 2018-05-06
      相关资源
      最近更新 更多