gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:18:13 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com