Table view cell load animation one after another

后端 未结 7 1006
面向向阳花
面向向阳花 2021-01-31 05:32

I need to animate the load of the table view rows. When the table reloads the data I need the rows get in from the left one after another. How can I achieve this?

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 06:11

    Here's my Swift 3 solution for displaying the cells one by one. what's nice about it is that they load only at first load time, and only for the initially displayed cells (will not run when user scrolls down).

    Enjoy :)

    private var finishedLoadingInitialTableCells = false
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
        var lastInitialDisplayableCell = false
    
        //change flag as soon as last displayable cell is being loaded (which will mean table has initially loaded)
        if yourTableData.count > 0 && !finishedLoadingInitialTableCells {
            if let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows,
                let lastIndexPath = indexPathsForVisibleRows.last, lastIndexPath.row == indexPath.row {
                lastInitialDisplayableCell = true
            }
        }
    
        if !finishedLoadingInitialTableCells {
    
            if lastInitialDisplayableCell {
                finishedLoadingInitialTableCells = true
            }
    
            //animates the cell as it is being displayed for the first time
            cell.transform = CGAffineTransform(translationX: 0, y: self.rowHeight/2)
            cell.alpha = 0
    
            UIView.animate(withDuration: 0.5, delay: 0.05*Double(indexPath.row), options: [.curveEaseInOut], animations: {
                cell.transform = CGAffineTransform(translationX: 0, y: 0)
                cell.alpha = 1
            }, completion: nil)
        }
    }
    

提交回复
热议问题