Jmyster
Jmyster

Reputation: 995

Adding animations at specific time intervals

I'm relatively new to animations so I'm looking for a bit of direction. Let's say I have a List<Label> with 15 label's loaded into it. I have DoubleAnimation that animates the opacity fro m 0 to 1 in 500 ms. I would like to loop through and these labels start their animation every 1000ms. So label1 starts at 0ms, label2 starts at 1000ms etc.

I know I can set a Storyboard.Duration to 1000ms*15 and add my labels to the SB and begin it, but the animations play as fast as they are added to the SB. Is there a way to add the animations to the SB at specific time intervals?

EDIT: I'm not using the List<Label> anymore Here is code that I wrote

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);
    }
}

Upvotes: 1

Views: 399

Answers (1)

Clemens
Clemens

Reputation: 128070

You could simply set the BeginTime of each animation.

And there's absolutely no need for a Storyboard. Just apply the animation to the Label:

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

And why that strange underscore on every variable name? There are widely accepted .Net naming conventions.

Upvotes: 3

Related Questions