UICollectionView 滚动缓进出效果

UICollectionView scroll ease in out effect

我正在尝试复制这个 behaviour。首先是单元格滚动的轻松。我做了类似的行为,但只有当单元格不显示并且将要显示时。我试图在屏幕上的单元格上有这种行为。我可能应该对某些 UIScrollView 代表执行此操作,但我需要一些指导。

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var array: [Holiday] = [Holiday(imageName: "a"),
                            Holiday(imageName: "b"),
                            Holiday(imageName: "c"),
                            Holiday(imageName: "d"),
                            Holiday(imageName: "e"),
                            Holiday(imageName: "f"),
                            Holiday(imageName: "g"),
                            Holiday(imageName: "h"),
                            Holiday(imageName: "i")]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView.backgroundColor = .white
        self.collectionView.register(CollectionCell.self)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: 176)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return collectionView.reusableCell(for: indexPath, with: self.array[indexPath.row]) as CollectionCell
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.array.count
    }

    override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

        cell.transform = CGAffineTransform(translationX: 0, y: cell.frame.height/2)

        UIView.animate(withDuration: 0.5, delay: 0.01 * Double(indexPath.row), options: [.curveEaseInOut], animations: {
            cell.transform = CGAffineTransform(translationX: 0, y: 0)
        }, completion: nil)
    }

}

对于此行为,您需要使用 UIDynamicAnimatorUICollectionViewFlowLayout

看看这个例子,也许它会对你有所帮助: https://www.cocoacontrols.com/controls/thspringycollectionview