1

我有以下代码将矩形向下平移 100px,然后一旦完成将矩形向右平移100px,但我希望向右平移以保持动画 Y (100px),但它不会这样做。调试时,我注意到 Translation 的值是 (0,0,0) 而不是 (0,100,0)。

这是我在我的应用程序中的代码(应用程序需要有 UWP 目标 + 最小 sdk 17763):

public MainPage()
{
    this.InitializeComponent();

    this.Loaded += (sender1, e1) =>
    {
        var compositor = Window.Current.Compositor;
        var batch = compositor.CreateScopedBatch(Windows.UI.Composition.CompositionBatchTypes.Animation);

        var vertical = compositor.CreateVector3KeyFrameAnimation();

        vertical.InsertKeyFrame(1.0f, new Vector3(0f, 100f, 0f));
        vertical.Duration = TimeSpan.FromMilliseconds(2000);
        vertical.Target = "Translation";

        Rect.StartAnimation(vertical);

        batch.End();

        batch.Completed += (sender2, e2) =>
        {
            var horizontal = compositor.CreateVector3KeyFrameAnimation();

            // PROBLEM: ... Rect.Translation.Y is not 100, it's 0 ...
            horizontal.InsertKeyFrame(1.0f, new Vector3(100f, Rect.Translation.Y, 0f));
            horizontal.Duration = TimeSpan.FromMilliseconds(2000);
            horizontal.Target = "Translation";

            Rect.StartAnimation(horizontal);
        };
    };
}

在此处输入图像描述

初始动画完成后保持 Y 位置的正确方法是什么?

4

1 回答 1

0

我很确定Johhny Westlake有一个有效的答案,但我用以下方法解决了我的问题:

this.Loaded += (sender1, e1) =>
{
    var compositor = Window.Current.Compositor;

    ElementCompositionPreview.SetIsTranslationEnabled(Rect, true);
    var visual = ElementCompositionPreview.GetElementVisual(Rect);
    var batch = compositor.CreateScopedBatch(Windows.UI.Composition.CompositionBatchTypes.Animation);

    var vertical = compositor.CreateScalarKeyFrameAnimation();

    vertical.InsertKeyFrame(1.0f, 100f);
    vertical.Duration = TimeSpan.FromMilliseconds(2000);

    visual.StartAnimation("Translation.Y", vertical);

    batch.End();

    batch.Completed += (sender2, e2) =>
    {
        var horizontal = compositor.CreateScalarKeyFrameAnimation();

        horizontal.InsertKeyFrame(1.0f, 100f);
        horizontal.Duration = TimeSpan.FromMilliseconds(2000);

        visual.StartAnimation("Translation.X", horizontal);
    };
};
于 2019-04-29T14:25:26.170 回答