gpt4 book ai didi

c# - 单击按钮上的 Xamarin.Forms 动画(Flash 背景)

转载 作者:行者123 更新时间:2023-12-02 14:33:39 25 4
gpt4 key购买 nike

我想在我的表单上实现拨号盘。现在,在我的 XAML 中我正在测试一个按钮:XAML

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyXFDemo001.Views.NewItemPage"
Title="New Item">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Save" Clicked="Save_Clicked" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout Spacing="20" Padding="15">
<Button
x:Name="buttonSelectContact"
Clicked="buttonSelectContact_Clicked"
Text="CLICK" BackgroundColor="#0080ff" />
</StackLayout>
</ContentPage.Content>

在我的代码后面,我有一个方法buttonSelectContact_Clicked:

private async void buttonSelectContact_Clicked(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.BackgroundColor = Color.FromHex("#22ac38");
await Task.Delay(500);
btn.BackgroundColor = Color.FromHex("#0080ff");
}

它可以工作,但并不像我想要的那样顺利。你能建议我添加动画而不是这个BackgroundColor吗?

最佳答案

我想更好的选择是创建一个可重用的功能来做到这一点。您可以利用 的动画资源.

例如,您可以创建一个扩展方法,为您提供一种在任何地方重复使用的好用且简单的方法,例如 FadeToTranslateTo...

这非常简单,除了在这种特殊情况下平滑改变颜色的逻辑之外。这是代码:

public static class AnimationExtensions
{
public static Task<bool> ChangeBackgroundColorTo(this Button self, Color newColor, uint length = 250, Easing easing = null)
{
Task<bool> ret = new Task<bool>(() => false);

if (!self.AnimationIsRunning(nameof(ChangeBackgroundColorTo)))
{
Color fromColor = self.BackgroundColor;

try
{
Func<double, Color> transform = (t) =>
Color.FromRgba(fromColor.R + t * (newColor.R - fromColor.R),
fromColor.G + t * (newColor.G - fromColor.G),
fromColor.B + t * (newColor.B - fromColor.B),
fromColor.A + t * (newColor.A - fromColor.A));

ret = TransmuteColorAnimation(self, nameof(ChangeBackgroundColorTo), transform, length, easing);
}
catch (Exception ex)
{
// to supress animation overlapping errors
self.BackgroundColor = fromColor;
}
}

return ret;
}

private static Task<bool> TransmuteColorAnimation(Button button, string name, Func<double, Color> transform, uint length, Easing easing)
{
easing = easing ?? Easing.Linear;
var taskCompletionSource = new TaskCompletionSource<bool>();

button.Animate(name, transform, (color) => { button.BackgroundColor = color; }, 16, length, easing, (v, c) => taskCompletionSource.SetResult(c));
return taskCompletionSource.Task;
}
}

导入此类(通过 using namespace 引用),您将能够像下面一样使用它。

声明 XAML 按钮:

<Button Text="Nice button ;)"
BackgroundColor="Gray"
x:Name="btnTeste"
Clicked="btnTest_Click"/>

在代码隐藏中处理点击:

private async void btnTest_Click(object sender, EventArgs args)
{
#region You will not need this block, it is just to choose a random color for change to
var colors = new[] { Color.Red, Color.Pink, Color.Silver, Color.Yellow, Color.Black, Color.Green };
var rnd = new Random();

var actualColor = btnTeste.BackgroundColor;
var randomColor = colors.Where(c => c != actualColor).ToArray()[rnd.Next(0, colors.Length - 2)];
#endregion

// Here is the effective use of the smooth background color change animation
await btnTeste.ChangeBackgroundColorTo(randomColor, 150, Easing.CubicOut);
await btnTeste.ChangeBackgroundColorTo(actualColor, 100, Easing.SinOut);
}

编辑:

这是结果(gif 显示了单击和双击,因此您可以看到很多平滑的更改):

effect example

关于c# - 单击按钮上的 Xamarin.Forms 动画(Flash 背景),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50367476/

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