Swift UICollectionView滚动易入易出效果

Swift UICollectionView滚动易入易出效果,swift,animation,uicollectionview,cgaffinetransform,Swift,Animation,Uicollectionview,Cgaffinetransform,我正试图复制这个。第一件事是易于在单元格中滚动。我做了一个类似的行为,但仅当单元格不显示并且将要显示时。我试图在屏幕上的细胞上有这种行为。我可能应该在某个UIScrollView代理上这样做,但我需要一些指导 class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { var array: [Holiday] = [Holiday(imageName: "a"),

我正试图复制这个。第一件事是易于在单元格中滚动。我做了一个类似的行为,但仅当单元格不显示并且将要显示时。我试图在屏幕上的细胞上有这种行为。我可能应该在某个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)
    }

}

对于此行为,您需要使用UIDynamicMator和UICollectionViewFlowLayout

看看这个例子,也许它会帮助你: