我有以下代码将矩形下移100px ,然后完成翻译,将矩形右100px 平移,但是我希望向右平移以保持动画Y (100像素),但是它不会那样做。调试时,我注意到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位置的正确方法是什么?
答案 0 :(得分: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);
};
};