【轉(zhuǎn)載】如何優(yōu)雅的實(shí)現(xiàn)TableView刷新動(dòng)效

作者:Dariel
原文鏈接:https://www.ctolib.com/topics-137696.html

1.簡單的實(shí)現(xiàn)

我們都知道 TableView 的刷新動(dòng)效是設(shè)置在 tableView(_:,willDisplay:,forRowAt:) 這個(gè)方法中的。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

        cell.alpha = 0;
        UIView.animate(withDuration: duration, delay: delayFactor * Double(indexPath.row), options: [], animations: {
            cell.alpha = 1
        }, completion: nil)

    }

這樣一個(gè)簡單的淡入效果就OK了,但這樣顯然不夠優(yōu)雅,我們?nèi)绻诙鄠€(gè)TableView使用這個(gè)效果該怎樣封裝呢?

2.使用工廠設(shè)計(jì)模式進(jìn)行封裝

2.1 creator(創(chuàng)建者):Animtor,用來傳入?yún)?shù)和設(shè)置動(dòng)畫

typealias Animation = (UITableViewCell, IndexPath, UITableView) -> ()

final class Animator {
    private var hasAnimatedAllCells = false
    private let animation: Animation

    init(animation: @escaping Animation) {
        self.animation = animation
    }

    func animate(cell: UITableViewCell, at indexPath: IndexPath, in tableView: UITableView) {
        guard !hasAnimatedAllCells else {
            return
        }

        animation(cell, indexPath, tableView)
    }
}

2.2 product(產(chǎn)品):AnimtionFactory,用來設(shè)置不同的動(dòng)畫類型

enum AnimationFactory {
    static func makeFade(duration: TimeInterval, delayFactor: Double) -> Animation {
        return {cell, indexPath, _ in
            cell.alpha = 0;
            UIView.animate(withDuration: duration, delay: delayFactor * Double(indexPath.row), options: [], animations: {
                cell.alpha = 1
            }, completion: nil)
        }
    }

    static func makeLeft() -> Animation {
        return {cell, indexPath, _ in
            cell.frame = CGRect(x: cell.frame.size.width, y: cell.frame.origin.y, width: cell.frame.size.width, height: cell.frame.size.height)
            UIView.animate(withDuration: 0.5, delay: 0.05 * Double(indexPath.row), options: [], animations: {
                cell.frame = CGRect(x: 0, y: cell.frame.origin.y, width: cell.frame.size.width, height: cell.frame.size.height)
            }, completion: nil)
        }
    }

    static func makeDamping() -> Animation {
        return {cell, indexPath, _ in
            cell.transform = CGAffineTransform(scaleX: 1, y: 0)
            UIView.animate(withDuration: 0.5, delay: 0.05, usingSpringWithDamping: 1, initialSpringVelocity: 25, options: [], animations: {
                cell.transform = CGAffineTransform(scaleX: 1, y: 1)
            }, completion: nil)
        }
    }

    static func makeDampingAndFade() -> Animation {
        return {cell, indexPath, _ in
            cell.transform = CGAffineTransform(scaleX: 1, y: 0)
            UIView.animate(withDuration: 0.5, delay: 0.05 * Double(indexPath.row), usingSpringWithDamping: 1, initialSpringVelocity: 25, options: [], animations: {
                cell.transform = CGAffineTransform(scaleX: 1, y: 1)
            }, completion: nil)
        }
    }
}

將所有的動(dòng)畫設(shè)置封裝在Animation的閉包中。
最后我們就可以在tableview:(_:,willDisplay:,forRowAt:)這個(gè)方法中使用如下代碼,就實(shí)現(xiàn)最終效果

let animation = AnimationFactory.makeFade(duration: 0.5, delayFactor: 0.05)
let animator = Animator(animation: animation)
animator.animate(cell: cell, at: indexPath, in: tableView)
最終效果

注:原作者在Animator中animate方法中有一句

hasAnimatedAllCells = tableview.isLastVisibleCell(at: indexPath)

我在tableview中并未找到對應(yīng)的方法,因此沒有這一句,貌似對效果沒有什么影響

Demo地址:https://github.com/allstar456/TableViewAnimationDemo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容