c# - WPF - 创建 float 动画可点击控件(图像或...)

标签 c# wpf floating animated suspend

enter image description here 我想创建一个 float 动画可点击控件,就像在我的 WPF 应用程序中有时也会向右或向左移动的氦气球。

The helium balloons likes go up! but also they moves right or left if we tap on them or by wind.

In advance cases, sometimes they turn to right or left

............................ enter image description here enter image description here enter image description here ...................................

所以我在网上搜索但没有找到任何有用的示例项目或库或样式。

  • 我如何在 WPF 中创建样式和动画以显示图像或 控制浮力或悬浮在空气中。?

  • 您有什么建议可以简单地实现这个想法......?

编辑:

  • 你对随机和无限平滑地向右和向左转有什么建议。例如,向左 51 度,然后向右 163 度,然后......我想把我的气球放在我的 window 里,并且通常在 window 的顶部

编辑:

我根据这些答案创建了一个动画,但通过操纵它们并在气球图像及其容器上添加多个并行动画而变得更加复杂。 ;) 谢谢大家...

现在我的初步结果是这样的: enter image description here

最佳答案

您可以使用 DoubleAnimationRotateTransform 来实现这一点,就像@Ega 提到的那样。要回答您的“随机和无限”向右和向左转弯,您可以按照以下方法进行操作(这是非常基本但可以完成的工作)。

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        var rnd = new Random();
        var direction = -1;

        while (true)
        {
            var percent = rnd.Next(0, 100);
            direction *= -1; // Direction changes every iteration
            var rotation = (int)((percent / 100d) * 45 * direction); // Max 45 degree rotation
            var duration = (int)(750 * (percent / 100d)); // Max 750ms rotation

            var da = new DoubleAnimation
            {
                To = rotation,
                Duration = new Duration(TimeSpan.FromMilliseconds(duration)),
                AutoReverse = true // makes the balloon come back to its original position
            };

            var rt = new RotateTransform();
            Balloon.RenderTransform = rt; // Your balloon object
            rt.BeginAnimation(RotateTransform.AngleProperty, da);

            await Task.Delay(duration * 2); // Waiting for animation to finish, not blocking the UI thread
        }
    }

编辑 .NET 3.5

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var thread = new Thread(this.AnimateBalloon);
        thread.Start();
    }

    public void AnimateBalloon()
    {
        var rnd = new Random();
        var direction = -1;

        while (true)
        {
            var percent = rnd.Next(0, 100);
            direction *= -1; // Direction changes every iteration
            var rotation = (int)((percent / 100d) * 45 * direction); // Max 45 degree rotation
            var duration = (int)(750 * (percent / 100d)); // Max 750ms rotation

            Balloon.Dispatcher.BeginInvoke(
                (Action)(() =>
                {
                    var da = new DoubleAnimation
                    {
                        To = rotation,
                        Duration = new Duration(TimeSpan.FromMilliseconds(duration)),
                        AutoReverse = true // makes the balloon come back to its original position
                    };

                    var rt = new RotateTransform();
                    Balloon.RenderTransform = rt; // Your balloon object
                    rt.BeginAnimation(RotateTransform.AngleProperty, da);
                }));

            Thread.Sleep(duration * 2);
        }
    }

关于c# - WPF - 创建 float 动画可点击控件(图像或...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28279094/

相关文章:

c# - 如何在不阻塞正在运行的线程的情况下创建应用程序全局进度窗口?

c# - 在 C# 中将数据 View 复制到数据表的最简单方法?

c# - StringBuilder 是否比 String 连接使用更多的内存?

c# - 在 Caliburn.micro 中设置初始窗口大小

c# - WPF 中的 DispatcherPriority

c# - 强制 WPF ListBox 恰好包含 x 个元素

c# - 混叠问题

xml - 使用 python-docx 编辑 float 文本框或编辑 document.xml?

html - 如何修复 float CSS 标签始终处于事件状态,在我的表单中显示 'invalid input'?

python - 如何将 float 转换为时分秒?