带延迟的 WPF 双动画 [英] WPF double animation with delay

查看:37
本文介绍了带延迟的 WPF 双动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将矩形的不透明度从 0 更改为 1,然后从 1 更改为 0,但还要在每一步等待 2 秒.所以我希望 2 秒为 1,然后在 500 毫秒内将其更改为 0,然后在不透明度为 0 的情况下再等待 2 秒,然后在 500 毫秒内再次变为 1,以此类推.

I am trying to change the opacity of a rectangle from 0 to 1 and back from 1 to 0, but also to wait 2 seconds at every step. so i want 2 seconds to be 1, after that to change it to 0 in 500 milliseconds and then wait another 2 seconds with the opacity 0 and going again in 500 milliseconds to 1 and so one.

我有这个代码:

        Storyboard.SetTargetProperty(forwardDoubleAnimation,
            new PropertyPath("(Path.Fill).(SolidColorBrush.Opacity)"));
        Storyboard.SetTargetProperty(reverseDoubleAnimation,
           new PropertyPath("(Path.Fill).(SolidColorBrush.Opacity)"));

        forwardDoubleAnimation.Completed += (sender, args) =>
        {
            Thread.Sleep(2000);
            reverseStoryboard.Begin();
        };

        reverseDoubleAnimation.Completed += (sender, args) =>
        {
            Thread.Sleep(2000);
            forwardStoryboard.Begin();
        };

但是我在 reverseStoryboard.Begin() 上有一个例外,说我没有为其设置目标属性.

But i have an exception on reverseStoryboard.Begin() that says i didn't set a target property for it.

有没有办法使用单个动画并设置属性以等待?

Is there a way to do it using a single animation and to set a property in order to wait?

推荐答案

您可以利用以下事实:尽管不透明度的值在 0 到 1 的范围内,但您可以将值设置为小于 0 或大于 1.

You may take advantage from the fact that, although an Opacity has values in the range 0 to 1, you may set values lower than 0 or greater than 1.

如果您在 2.5 秒内将不透明度从 -2 设置为 3,则在第一秒内它从 -2 变为 0,实际上是 0,然后它在 0.5 秒内从 0 变为 1,然后从 1 变为 3, 即在另一秒内有效 1.

If you animate an Opacity from -2 to 3 in 2.5 second, in the first second it goes from -2 to 0, which is 0 effectively, then it goes from 0 to 1 in 0.5 seconds, then from 1 to 3, i.e. effectively 1 in another second.

var animation = new DoubleAnimation
{
    From = -2,
    To = 3,
    Duration = TimeSpan.FromSeconds(2.5),
    AutoReverse = true,
    RepeatBehavior = RepeatBehavior.Forever
};

rectangle.Fill.BeginAnimation(Brush.OpacityProperty, animation);

这篇关于带延迟的 WPF 双动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆