How to I detect when an animation is stopped and properties updated?

对着背影说爱祢 提交于 2019-12-13 02:36:18

问题


If I create and start an animation in the visual layer, I could use StopAnimation to stop the animation. But when I do that it seems that it takes a while before the animation is stopped and the properties are updated with the latest values. Is there some way around that? In the code below, I wait 10 milliseconds but that only works sometimes.

public sealed partial class MainPage : Page
{
    SpriteVisual MyVisual;

    public MainPage()
    {
        this.InitializeComponent();

        PointerReleased += MainPage_PointerReleased;
    }


    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        var compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;

        MyVisual = compositor.CreateSpriteVisual();

        MyVisual.Size = new Vector2(80, 80);
        MyVisual.Offset = new Vector3(50, 50, 0);
        MyVisual.Brush = compositor.CreateColorBrush(Colors.Green);

        ElementCompositionPreview.SetElementChildVisual(this, MyVisual);

        var animation = compositor.CreateVector3KeyFrameAnimation();
        animation.InsertKeyFrame(1, new Vector3(300, 50, 0));
        animation.Duration = TimeSpan.FromSeconds(3);
        animation.IterationBehavior = AnimationIterationBehavior.Forever;

        MyVisual.StartAnimation(nameof(MyVisual.Offset), animation);
    }

    private async void MainPage_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Position when animation running: " + MyVisual.Offset);

        MyVisual.StopAnimation(nameof(MyVisual.Offset));

        await Task.Delay(10);

        System.Diagnostics.Debug.WriteLine("Position when animation stopped: " + MyVisual.Offset);
    }
}

回答1:


You could use CompositionScopedBatch to solve this. One thing to notice is that the Completed event is triggered immediately if you have infinite number of iterations (I have no idea why). Here is a sample:

public sealed partial class MainPage : Page
{
    SpriteVisual MyVisual;
    CompositionScopedBatch ScopedBatch;

    public MainPage()
    {
        this.InitializeComponent();

        PointerReleased += MainPage_PointerReleased;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        var compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;

        MyVisual = compositor.CreateSpriteVisual();

        MyVisual.Size = new Vector2(80, 80);
        MyVisual.Offset = new Vector3(50, 50, 0);
        MyVisual.Brush = compositor.CreateColorBrush(Colors.Green);

        ElementCompositionPreview.SetElementChildVisual(this, MyVisual);

        ScopedBatch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);

        var animation = compositor.CreateVector3KeyFrameAnimation();
        animation.InsertKeyFrame(1, new Vector3(300, 50, 0));
        animation.Duration = TimeSpan.FromSeconds(3);
        // animation.IterationBehavior = AnimationIterationBehavior.Forever;
        // CompositionScopedBatch.Completed is triggered immediately if Infinite is used.
        // Use many iterations instead.
        animation.IterationBehavior = AnimationIterationBehavior.Count;
        animation.IterationCount = 1000000;

        MyVisual.StartAnimation(nameof(MyVisual.Offset), animation);

        ScopedBatch.Completed += CompositionScopedBatch_Completed;

        ScopedBatch.End();
    }

    private void CompositionScopedBatch_Completed(object sender, CompositionBatchCompletedEventArgs args)
    {
        System.Diagnostics.Debug.WriteLine("Position when batch completed: " + MyVisual.Offset);
    }

    private void MainPage_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Position when animation running: " + MyVisual.Offset);

        MyVisual.StopAnimation(nameof(MyVisual.Offset));
    }
}


来源:https://stackoverflow.com/questions/47236441/how-to-i-detect-when-an-animation-is-stopped-and-properties-updated

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!