2012-08-02 86 views
3

我正在使用MonoDevelop和xamarin創建iOS應用程序。iOS,Xamarin,UIView,繪製,動態動畫不起作用

我有一個觀點,看起來像這樣:

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

我希望我的形象會移動。但結果我只有初始靜態圖像。任何人都可以建議如何做動畫嗎?

回答

1

我建議修改,像這樣的代碼:

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

我覺得你的問題的一部分是,定時器經過的處理上比主UI線程獨立的線程中運行。此外,您每次都不要重新創建UIImageView會更高效。