UICollectionView滚动缓入效果

时间:2018-12-05 18:08:20

标签: swift animation uicollectionview cgaffinetransform

我正在尝试复制此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)
    }

}

1 个答案:

答案 0 :(得分:0)

为此,您需要使用UIDynamicAnimatorUICollectionViewFlowLayout

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