2013-03-26 36 views
2

我試圖在TableView單元格中設置一系列圖標圖像的動畫,並且我無法使ImageView.StartAnimating正常工作。 TableView位於UITableViewController中,並且有一個TableViewDataSource文件用於設置TableView內容。爲什麼ImageView.StartAnimating不會在主線程上動畫

在我的TableViewDataSource中,我設置了PNG數組並配置了各種動畫首選項。

UIImage image0 = GetImage("Images/[email protected]"); 
UIImage image45 = GetImage("Images/[email protected]"); 
UIImage image90 = GetImage("Images/[email protected]"); 
UIImage image135 = GetImage("Images/[email protected]"); 
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 

如果我請StartAnimating在TableViewDataSource(在上面的示例註釋)的圖像中的動畫序列。

但它在我的UITableViewController中,我希望圖像在響應某個特定事件時被激活。

在viewWillAppear中我得到特定小區的ImageView的

UITableViewCell cell = TableView.CellAt(NSIndexPath.FromRowSection(0, 2)); 
imagetoAnimate = (UIImageView)cell.ContentView.ViewWithTag(ICON_TAG); 

然後,當事件引發的參考我打電話StartAnimating在主線程(我加了Console.WriteLines只是爲了確認裏面的代碼InvokeOnMainThread塊確實執行主線程上 - 這也是所有的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(); 
    }); 
} 

這一定是一些簡單的我失蹤。如果您需要更多信息,請與我們聯繫。

在此先感謝。

UPDATE#1

使用UpdateCount和UpdateStatus方法含有看起來與ImageView.StartAnimating會干擾TableView.ReloadData語句(註釋出兩條線允許動畫進行)。

有沒有人聽說過這種類型的事情發生之前?

更新#2

移動碼塊,設置ImageView.AnimationImages出GetCell方法並進入的UITableViewController解決了問題。

UIImage image0 = GetImage("Images/[email protected]"); 
UIImage image45 = GetImage("Images/[email protected]"); 
UIImage image90 = GetImage("Images/[email protected]"); 
UIImage image135 = GetImage("Images/[email protected]"); 
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時動畫正常進行。

+0

如果你在你調用'imageToAnimate.StartAnimating()'的地方放置了一個斷點,它是非空的並且具有所有適當的值,例如'AnimationImages'填充了,對嗎? – jonathanpeppers 2013-03-26 16:58:31

+0

是的,imageToAnimate不爲null,AnimationImages包含四個圖像,isAnimating = false(當StartAnimating調用時,動畫會翻轉爲true)。 – billmaya 2013-03-26 18:18:06

+0

GetCell被調用或表視圖後立即重新加載? – jonathanpeppers 2013-03-26 18:26:08

回答

0

調用UITableView.ReloadData將重新加載所有的單元格,並且GetCell將再次在您的表格視圖源中調用。

這可能會停止發生動畫。

相關問題