2011-08-26 67 views
16

如何知道[UITableView setEditing:YES animated:YES]何時完成?如何判斷UITableView動畫何時結束?

我不想給出任何上下文,因爲我想避免任何人給我解決方法,這些我不感興趣。

我想要的是調用上述,然後在動畫完成時調用一個單獨的函數。


我被編輯後給出的上下文和一些解決方法。 最初我setEditing並立即重新加載表數據。

[tableView setEditing:YES animated:YES]; 
[tableView reloadData]; 

問題是表在動畫開始之前重新加載,所以動畫從未出現。

這裏有一些不同的解決方法:

[tableView setEditing:YES animated:YES]; 
[self performSelector:@selector(ReloadTable) withObject:nil afterDelay:1.0]; 

這工作,但如果我得到的延遲不正確,它會看起來很糟糕。所以我需要知道延遲是什麼,我可以弄清楚,但我們不敢保證延遲總是一樣的。

isEditing = YES; 
[tableView reloadData]; 
[tableView setEditing:YES animated:YES]; 

這可以工作,但表格的行爲有所不同,取決於我們是否處於編輯模式。所以我必須使用我自己的isEditing變量而不是標準的UITableView.editing。我寧願不必創建一個新的布爾isEditing變量。

[tableView setEditing:YES animated:YES]; 
[tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationTop]; 

這幾乎是效果很好,但在編輯模式下的第一行應該有UITableViewCellEditingStyleInsert,而其他行獲得UITableViewCellEditingStyleDelete。通過上面的代碼,在添加行之前,編輯樣式會被設置。因此第二行以UITableViewCellEditingStyleInsert結束。

回答

46
[CATransaction begin]; 
[CATransaction setCompletionBlock: ^{ 
    // your animation has finished 
}]; 
[tableView setEditing:YES animated:YES]; 
[CATransaction commit]; 

注意setCompletionBlock必須在頂部。

+2

+1這工作*完美*。 – Isaac

+2

我已經嘗試了很多關於這個問題的其他線程的建議,這是第一個工作的! – Marty

+0

真是個好主意! – Swindler

3

在iOS 4的,你可以做到以下幾點:

[UIView animateWithDuration:0.3f 
       animations:^{ 
        [self.tableView setEditing:YES animated:NO]; 
       } 
       completion:^(BOOL finished){ 
        // Do something 
       } 
]; 
+0

這種運作良好(+1),但在另一個答案'CATransaction'建議更好地工作。 – Isaac

+0

這取決於Apple爲動畫選擇的時間。如果他們在未來的版本中更改時間,那麼這將不再適用。查看接受的答案以獲得更好的方式。 – fishinear

1

斯威夫特4版本接受的答案:

CATransaction.begin() 
CATransaction.setCompletionBlock { 
    // your animation has finished 
} 
tableView.setEditing(true, animated: true) 
CATransaction.commit()