制作标签动画而不重置超时持续时间



我有以下方法来为标签设置20秒的动画。

public class BlinkTriggerBehavior : Behavior<Label>
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
protected override void OnAttachedTo(Label bindable)
{
base.OnAttachedTo(bindable);
bindable.PropertyChanged += Animate; // binding property changed event
}
private async void Animate(object obj, PropertyChangedEventArgs e)
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
if (e.PropertyName == "ClassId")
{
VisualElement sender = obj as VisualElement;
var parentAnimation = new Animation();
var scaleUpAnimation = new Animation(d => sender.Scale = d, 1, 1.2, Easing.SpringIn);
var fadeOutAnimation = new Animation(d => sender.Opacity = d, 1, 0, Easing.Linear);
var scaleDownAnimation = new Animation(d => sender.Scale = d, 1.2, 1, Easing.SpringOut);
var fadeInAnimation = new Animation(d => sender.Opacity = d, 0, 1, Easing.Linear);
parentAnimation.Add(0, 0.5, fadeOutAnimation);
parentAnimation.Add(0, 0.5, scaleUpAnimation);
parentAnimation.Add(0.5, 1, scaleDownAnimation);
parentAnimation.Add(0.5, 1, fadeInAnimation);
parentAnimation.Commit(sender, "BlinkingVisualElement", 16, 800, repeat: () => true);
await Task.Delay(20000);
parentAnimation.Commit(sender, "BlinkingVisualElement", 16, 800, repeat: () => false);
}
}
protected override void OnDetachingFrom(Label bindable)
{
base.OnDetachingFrom(bindable);
bindable.PropertyChanged -= Animate;
}
}

此方法可以在20秒内被调用多次。它运行良好,如果调用一次,则会为标签设置整整20秒的动画,并等待20秒后再调用第二次。我希望每当这个方法启动时,它都能设置20秒的动画。

现在发生了什么

我第一次在上调用该方法

上午9:00:00

它的动画直到9:00:20

如果我在上第二次调用该方法

上午9:00:12

它仍然设置动画,直到9:00:20(仅8秒(。我希望它动画化到9:00:32

基本上,每当调用此方法时,我都想重置计时器并显示完整的20秒动画。

您可以使用System.Timers.Timer在延迟一段时间后执行任务,并重置计时器以重置延迟:

public partial class MainPage : ContentPage
{
System.Timers.Timer Timer1 { get; set; }
int count = 0;
public MainPage()
{
InitializeComponent();
}

private async void start(object sender, EventArgs e)
{
//VisualElement sender = obj as VisualElement;
//var parentAnimation = new Animation();
//var fadeOutAnimation = new Animation(d => sender.Opacity = d, 1, 0, Easing.Linear);
//var fadeInAnimation = new Animation(d => sender.Opacity = d, 0, 1, Easing.Linear);
//parentAnimation.Add(0, 0.5, fadeOutAnimation);
//parentAnimation.Add(0.5, 1, fadeInAnimation);
//parentAnimation.Add(0, 0.5, fadeOutAnimation);
//parentAnimation.Add(0.5, 1, fadeInAnimation);
//parentAnimation.Add(0, 0.5, fadeOutAnimation);
//parentAnimation.Add(0.5, 1, fadeInAnimation);
//parentAnimation.Add(0, 0.5, fadeOutAnimation);
//parentAnimation.Add(0.5, 1, fadeInAnimation);
//parentAnimation.Commit(sender, "BlinkingVisualElement", 16, 800, repeat: () => true);
Console.WriteLine("start");
if (Timer1 != null)
{
Timer1.Stop();
Timer1.Dispose();
}
Timer1 = new System.Timers.Timer();
//count every 1 second
Timer1.Interval = 1000;
Timer1.Enabled = true;
//reset count
count = 0;
Timer1.Start();
Timer1.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
{
Device.BeginInvokeOnMainThread(() =>
{
count++;
//10 here is the delay time
if (count == 20)
{
Console.WriteLine("finish");

//parentAnimation.Commit(sender, "BlinkingVisualElement", 16, 800, repeat: () => false);
Timer1.Stop();
Timer1.Dispose();
}
else
{
Console.WriteLine("current count(seconds):" + count);
}
});
};
}   
}

最新更新