以特定时间间隔添加动画
本文关键字:添加 动画 定时间 | 更新日期: 2023-09-27 18:30:23
我对动画比较陌生,所以我正在寻找一些方向。假设我有一个装有 15 个标签的List<Label>
。我有DoubleAnimation
在 0 毫秒内将 m 1 的不透明度动画化为 500。我想循环播放,这些标签每 1000 毫秒开始一次动画。所以标签 1 从 0ms 开始,标签 2 从 1000ms 开始,依此类推。
我知道我可以将Storyboard.Duration
设置为 1000ms*15,并将我的标签添加到 SB 并开始它,但动画的播放速度与添加到 SB 的速度一样快。有没有办法以特定的时间间隔将动画添加到 SB
编辑:我不再使用List<Label>
了这是我写的代码
class MessageWriter
{
Storyboard _sb = new Storyboard();
public MessageWriter()
{
}
public void ProcessMessage(string _Message, WrapPanel _Panel)
{
string[] _Words = _Message.Split(new char[1]{Convert.ToChar(" ")});
int _Counter = 1;
_sb = new Storyboard();
foreach (string _Word in _Words)
{
_Panel.Children.Add(ProcessWord(_Word));
if (_Counter < _Words.Length)
{
_Panel.Children.Add(ProcessWord(" "));
}
_Counter++;
}
_sb.Begin();
}
private StackPanel ProcessWord(string _Word)
{
Debug.Print(_Word);
StackPanel _Panel = new StackPanel();
_Panel.Orientation = Orientation.Horizontal;
_Panel.Margin = new System.Windows.Thickness(0, 0, 0, 0);
foreach (char _c in _Word.ToList())
{
Label _Label = new Label();
_Label.Opacity = 0;
_Label.Margin = new System.Windows.Thickness(0, 0, 0, 0);
_Label.Padding = new System.Windows.Thickness(0, 0, 0, 0);
_Label.Content = _c.ToString();
_Panel.Children.Add(_Label);
AnimateLabel(_Label);
}
return _Panel;
}
private void AnimateLabel(Label _Label)
{
DoubleAnimation _da = new DoubleAnimation();
_da.From = 0;
_da.To = 1;
int _MillSec = 500;
_da.Duration = new System.Windows.Duration(new TimeSpan(0, 0, 0, 0, _MillSec));
Storyboard.SetTargetProperty(_da, new PropertyPath(FrameworkElement.OpacityProperty));
Storyboard.SetTarget(_da, _Label);
_sb.Children.Add(_da);
}
}
您可以简单地设置每个动画的开始时间。
而且绝对不需要故事板。只需将动画应用于标签:
DoubleAnimation opacityAnimation = new DoubleAnimation
{
To = 1d,
Duration = TimeSpan.FromSeconds(0.5),
BeginTime = TimeSpan.FromSeconds((double)labelIndex)
};
labelIndex++;
label.BeginAnimation(UIElement.OpacityProperty, opacityAnimation);
为什么每个变量名称上都有奇怪的下划线?有广泛接受的 .Net 命名约定。