WPF. Pattern animation class

家住魔仙堡 提交于 2020-05-17 08:50:48

问题


[I created a DrawingLine animation class to draw patterns.

The constructor starts the process of creating and animating lines:

internal DrawingLine(double x, double y, int _thickness, Brush _brush, Canvas _canvas)

At the end of the animation, this method generates a new line:

void CreateNewLine(object sender, EventArgs e)
        {
            Line newLine = new Line();

            switch (lines.Count % 2 == 0)
            {
                case true:
                    {
                        if (lines.Count < 18)
                        {
                                    newLine.X1 = 0;
                                    newLine.Y1 = 180 - offset;
                                    newLine.X2 = 0;
                                    newLine.Y2 = 180 - offset;
                        }

                        //   ...   <-- Here is the math determines the positions of the points of the line

        }

The method produces animation:

void AnimateXY()
        {
            DoubleAnimation lineXAnimation = new DoubleAnimation();
            lineXAnimation.From = CurrentLine.X1;
            lineXAnimation.To = to_X;
            lineXAnimation.Duration = TimeSpan.FromSeconds(duration);

            DoubleAnimation lineYAnimation = new DoubleAnimation();
            lineYAnimation.From = CurrentLine.Y1;
            lineYAnimation.To = to_Y;
            lineYAnimation.Duration = TimeSpan.FromSeconds(duration);
            lineYAnimation.Completed += CreateNewLine;

            CurrentLine.BeginAnimation(Line.X2Property, lineXAnimation);
            CurrentLine.BeginAnimation(Line.Y2Property, lineYAnimation);
        }

Question: What is the best way to loop this animation, so that there are no memory leaks? I would also like to hear general recommendations for improving the structure of such classes. Picture of the pattern is attached.]1


回答1:


I found the answer to my question. If the class is full of animation, then it supports a recursive call (recursive creation). A recursive call makes the animation endless.



来源:https://stackoverflow.com/questions/61678709/wpf-pattern-animation-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!