ios - 为什么 ImageView.StartAnimating 不会在主线程上设置动画

标签 ios multithreading animation xamarin.ios uiimageview

我正在尝试为 TableView 单元格中的一系列图标图像设置动画,但无法让 ImageView.StartAnimating 工作。 TableView 位于 UITableViewController 中,并且有一个 TableViewDataSource 文件,其中设置了 TableView 内容。

在我的 TableViewDataSource 中,我设置了 PNG 数组并配置了各种动画首选项。

UIImage image0 = GetImage("Images/image_at_0_degrees@2x.png");
UIImage image45 = GetImage("Images/image_at_45_degrees@2x.png");
UIImage image90 = GetImage("Images/image_at_90_degrees@2x.png");
UIImage image135 = GetImage("Images/image_at_135_degrees@2x.png");
UIImage[] images = new UIImage[] { image0, image135, image90, image45 };

cell.ImageView.AnimationImages = images;
cell.ImageView.AnimationDuration = 0.5;
cell.ImageView.AnimationRepeatCount = 0;
cell.ImageView.Image = image0;

// cell.ImageView.StartAnimating(); // If called here images will animate

如果我在 TableViewDataSource 中调用 StartAnimating(在上面的示例中注释掉),图像会按顺序进行动画处理。

但它在我的 UITableViewController 中,我希望图像动画以响应某个事件被触发。

在 ViewWillAppear 中,我得到了对特定单元格的 ImageView 的引用
UITableViewCell cell = TableView.CellAt(NSIndexPath.FromRowSection(0, 2));
imagetoAnimate = (UIImageView)cell.ContentView.ViewWithTag(ICON_TAG);

然后当事件引发时,我在主线程上调用 StartAnimating(我添加了 Console.WriteLines 只是为了确认 InvokeOnMainThread block 内的代码确实在主线程上执行 - 它是。此外,InvokeOnMainThread 内的所有其他代码块以前在那里。)
private void Application_Event()
{      
    Console.WriteLine("Running on Thread ID {0}. Is Main thread? {1}", Thread.CurrentThread.ManagedThreadId, NSThread.IsMain); // Returns false

    InvokeOnMainThread(delegate 
    { 

        Console.WriteLine("Running on Thread ID {0}. Is Main thread? {1}", Thread.CurrentThread.ManagedThreadId, NSThread.IsMain); // Returns true

        imageToAnimate.StartAnimating(); // If called here images don't animate
        UpdateCount();
        UpdateStatus(); 
    });
}

我想念的东西一定很简单。如果您需要更多信息,请与我们联系。

提前致谢。

更新 #1

UpdateCount 和 UpdateStatus 方法包含似乎干扰 ImageView.StartAnimating 的 TableView.ReloadData 语句(注释掉这两行允许动画继续进行)。

有没有人听说过这种事情发生过?

更新 #2

将设置 ImageView.AnimationImages 的代码块从 GetCell 方法移到 UITableViewController 中可以解决该问题。
UIImage image0 = GetImage("Images/image_at_0_degrees@2x.png");
UIImage image45 = GetImage("Images/image_at_45_degrees@2x.png");
UIImage image90 = GetImage("Images/image_at_90_degrees@2x.png");
UIImage image135 = GetImage("Images/image_at_135_degrees@2x.png");
UIImage[] images = new UIImage[] { image0, image135, image90, image45 };

cell.ImageView.AnimationImages = images;
cell.ImageView.AnimationDuration = 0.5;
cell.ImageView.AnimationRepeatCount = 0;
cell.ImageView.Image = image0;

完成此操作并且取消注释 TableView.ReloadData 语句后,动画会在调用 ImageView.StartAnimating 时正常进行。

最佳答案

调用UITableView.ReloadData将重新加载所有单元格,GetCell将在您的表格 View 源上再次被调用。

这可能会阻止动画的发生。

关于ios - 为什么 ImageView.StartAnimating 不会在主线程上设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15641064/

相关文章:

ios - 未调用 didDetermineState、didEnterRegion、didExitRegion 事件

multithreading - 实时多线程最大堆,用于top-N geohash

c# - 等待池线程完成

c - 从单独的线程调用时 pselect 不返回信号,但在单线程程序中工作正常

ios - 将应用程序从一个 Facebook 开发者帐户转移到另一个 Facebook 开发者帐户

ios - UIGestureRecognizerDelegate 来管理手势——我可以解除触摸分配吗?

ios - ld : framework not found Crashlytics

android - 顺序动画 XML 中的错误

ios - 从左到右将 UIView 移出屏幕并从左再次将其移入

html - Angular 淡入淡出动画不起作用