簡體   English   中英

UITableView 單元格滾動動畫。 迅速

[英]UITableView cells animation on scroll. Swift

社區,請幫助初學者! 你能告訴我如何為 TableView 單元格制作這樣的動畫嗎? 感謝!

使用 Swift 制作本視頻中的單元格動畫

這給出了類似於你想要的東西。 您可能需要對其進行調整或改進代碼片段。 您可以使用 tableView 的panGestureRecognizer屬性

tableView.panGestureRecognizer.addTarget(self, action: #selector(didPan(_:)))

使用此處理程序:


    @objc func didPan(_ gesture: UIPanGestureRecognizer) {
        guard gesture.state == .changed else { return }

        let loc = gesture.location(in: tableView)
        guard let touchedCellIndex = tableView.indexPathForRow(at: loc) else { return }
        let n = tableView.numberOfRows(inSection: 0)
        guard touchedCellIndex.row < n - 1 else { return }
        
        if gesture.translation(in: tableView).y < 0 {
            for (index, cell) in tableView.visibleCells.enumerated() {
                guard let ip = tableView.indexPath(for: cell) else { return }
                if ip.row > touchedCellIndex.row {
                    cell.transform = CGAffineTransform(translationX: 0.0, y: -gesture.velocity(in: tableView).y * 0.02 * tanh(CGFloat(index)))
                    UIView.animate(withDuration: 0.05 * Double(index), delay: 0.0) {
                        cell.transform = CGAffineTransform.identity
                    }
                }
                
            }
        } else {
            for (index, cell) in tableView.visibleCells.reversed().enumerated() {
                guard let ip = tableView.indexPath(for: cell) else { return }
                if ip.row < touchedCellIndex.row {
                    cell.transform = CGAffineTransform(translationX: 0.0, y: -gesture.velocity(in: tableView).y * 0.02 * tanh(CGFloat(index)))
                    UIView.animate(withDuration: 0.05 * Double(index), delay: 0.0) {
                        cell.transform = CGAffineTransform.identity
                    }
                }
                
            }
        }
    }
     

我相信您可以優化這種方法並使其更簡潔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM