[英]How to check if a cell has been displayed Swift
當我顯示我的tableview時,我已經讓我的單元格帶有這樣的動畫。
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.alpha = 0
let rotation = CATransform3DTranslate(CATransform3DIdentity, -250, 20, 0)
cell.layer.transform = rotation
UIView.animateWithDuration(0.9){
cell.alpha = 1
cell.layer.transform = CATransform3DIdentity
}
它的工作方式與此類似,但是當我向下然后再次查看之前的單元格時,動畫仍然存在。
顯示我相信我必須檢查是否已顯示該單元格。
我嘗試的是使用didEndDisplayingCell函數並將cell.tag放在一個數組中,然后我檢查當前單元格是否在array.contains(cell.tag)的數組中但是它不起作用。 實際上,動畫只適用於三個第一個單元格,然后什么也沒有。
您應該保留一個indexPaths數組,每當顯示一個單元格時添加它。 這樣您就可以檢查單元格是否已經設置了動畫。
if (!indexPaths.contains(indexPath)) {
indexPaths.append(indexPath)
cell.alpha = 0
let rotation = CATransform3DTranslate(CATransform3DIdentity, -250, 20, 0)
cell.layer.transform = rotation
UIView.animateWithDuration(0.9){
cell.alpha = 1
cell.layer.transform = CATransform3DIdentity
}
}
正如@ James-P在評論中提到的 - 只需創建一個數組
var indexPathArray = [NSIndexPath]()
並重寫你的willDisplayCell方法:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if !indexPathArray.contains(indexPath) {
indexPathArray.append(indexPath)
cell.alpha = 0
let rotation = CATransform3DTranslate(CATransform3DIdentity, -250, 20, 0)
cell.layer.transform = rotation
UIView.animateWithDuration(0.9){
cell.alpha = 1
cell.layer.transform = CATransform3DIdentity
}
}
}
你可以做的是將單元格的索引路徑放在一個數組中,並檢查索引路徑是否在數組中。如果索引路徑在數組中,則不執行動畫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.