ios任务,核心图形动画

时间:2016-12-29 22:29:17

标签: c# ios .net multithreading xamarin

最近我和xamarin IDE一起工作,我已经编写了一些基础知识,可以说是动画'。在这样的另一个任务中更新UIView是一种好的编码方式吗?我写了一个类UIBackgroundView类,它通过给定的路径生成CoreGraphics圆圈,并且覆盖了#34; Draw"方法。

public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.
        backgroundView.BackgroundColor = new UIColor(0, 0, 0, 0);
        backgroundView.arcs = new List<Arc>();
        for (int n = 0; n < 10; n++)
        {
            x = rand.Next(0, (int)backgroundView.Bounds.Width);
            y = rand.Next(0, (int)backgroundView.Bounds.Height);
            backgroundView.arcs.Add(new Arc(x, y, 7 * n, 0, (nfloat)(2 * Math.PI), true,
                                            new CGColor(0,
                                                        (float)rand.Next(70, 100) / 100,
                                                        (float)rand.Next(70, 100) / 100, 0.2f), (float)rand.Next(50, 300) / 100));
        }
        backgroundView.SetNeedsDisplay();
        Task task = new Task(generateCircles);
        task.Start();
    }

    void generateCircles()
    {

        while (true)
        {
            for (int n = 0; n < 10; n++)
            {
                if (backgroundView.arcs[n].y + backgroundView.arcs[n].radius > 0)
                    backgroundView.arcs[n].y -= backgroundView.arcs[n].speed;
                else
                {
                    backgroundView.arcs[n].y = backgroundView.Bounds.Height + backgroundView.arcs[n].radius;
                    backgroundView.arcs[n].color = new CGColor(0, (float)rand.Next(70, 100) / 100, (float)rand.Next(70, 100) / 100, 0.2f);
                }
            }
            InvokeOnMainThread(() => backgroundView.SetNeedsDisplay());
            Thread.Sleep(10);
        }

    }

0 个答案:

没有答案
相关问题