我正在使用MonoDevelop和xamarin来创建iOS应用程序。
我有一个看起来像这样的观点:
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);
}
}
我希望我的图像会移动。但结果我只是初始静态图像。任何人都可以建议如何处理如上所述的动画吗?
答案 0 :(得分: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;
});
}
我认为您的部分问题是计时器Elapsed处理程序在与主UI线程不同的线程上运行。另外,每次重新创建UIImageView都会更有效率。