简体   繁体   English

iOS,Xamarin,UIView,Draw,动态动画不起作用

[英]iOS, Xamarin, UIView, Draw, dynamic animation doesn't works

I'm using MonoDevelop and xamarin for creating iOS application. 我正在使用MonoDevelop和xamarin来创建iOS应用程序。

I have a view which looks like this: 我有一个看起来像这样的视图:

public class MyView: UIView 
{
    private Timer _t = new Timer(1000);
    private int _i = 0;

    public MyView()
    {
        _t.Elapsed += (sender, e) => { 
             _i = (_i + 1) % 100;
             this.Draw(new Rectangle(0, 0, 110, 110)); 
         };
        _t.Enabled = true;
    }

    public override void Draw(RectangleF rect)
    {
        base.Draw(rect);
        var uiImage = UIImage.FromFile("myImage");
        var uiImageView = new UIIMageView(new Rectangle(_i, 0, 10, 10));
        uiImageView.Image = uiImage;
        AddSubView(uiIMageView);
    }
}

I expect that my image will move. 我希望我的形象能够移动。 But as a result I've only initial static image. 但结果我只是初始静态图像。 Can anybody suggest approach how to do animation like described? 任何人都可以建议如何处理如上所述的动画?

I would suggest modifying your code like so: 我建议像这样修改你的代码:

public MyView()
    {

        var uiImage = UIImage.FromFile("myImage");
        var uiImageView = new UIIMageView(new Rectangle(0, 0, 10, 10));
        uiImageView.Image = uiImage;
        AddSubView(uiIMageView);

        _t.Elapsed += (sender, e) => { 
             _i = (_i + 1) % 100;
             this.Draw(new Rectangle(_i, 0, 110, 110)); 
         };
        _t.Enabled = true;
    }

    public override void Draw(RectangleF rect)
    {
        base.Draw(rect);
        InvokeOnMainThread(()=>{
             uiImageView.Frame = rect;
        });
    }

I think part of your issue was that the timer Elapsed handler runs on a separate thread than the main UI Thread. 我认为你的问题的一部分是计时器Elapsed处理程序在一个独立的线程上运行,而不是主UI线程。 Also you it'll be more efficient to not recreate the UIImageView each time. 另外,每次都不重新创建UIImageView会更有效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM