2012-04-06 84 views
1

我对动画比较新,所以我正在寻找一些方向。比方说,我有一个List<Label>加载了15个标签。我有DoubleAnimation在500毫秒内将不透明度从0移动到1。我想循环播放,这些标签每1000毫秒开始动画。所以label1从0ms开始,label2从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); 
    } 
} 

回答

3

你可以简单地设置每个动画的BeginTime

绝对不需要故事板。只需将动画应用到标签:

DoubleAnimation opacityAnimation = new DoubleAnimation 
{ 
    To = 1d, 
    Duration = TimeSpan.FromSeconds(0.5), 
    BeginTime = TimeSpan.FromSeconds((double)labelIndex) 
}; 
labelIndex++; 
label.BeginAnimation(UIElement.OpacityProperty, opacityAnimation); 

为什么那个奇怪的下划线上的每个变量的名字吗?有广泛接受的.Net naming conventions

+0

解决了这个问题。谢谢。如果有人想要详细说明如何使用故事板做到这一点,将不胜感激。将来我需要在两者之间做一些工作,所以我需要先制作时间线,然后在完成时播放。 – Jmyster 2012-04-06 22:31:09

+0

这也适用于您添加到故事板的动画。这只是更简单。 – Clemens 2012-04-06 22:32:18

+0

下划线是我拾起的坏习惯。我很肛门,需要为我的所有变量都有相同的前缀。我还没有问题,所以我从来没有改变。 – Jmyster 2012-04-06 23:04:00