如何在诸如App Store的集合视图单元中使UICollectionView动画化?

时间:2019-12-27 09:22:09

标签: ios swift uicollectionview

如何实现以下目标:

enter image description here 即使在我滚动浏览时,收藏夹视图也会在收藏夹视图单元格内轻轻移动。

3 个答案:

答案 0 :(得分:0)

您可以使用CollectionKit框架进行操作 https://github.com/SoySauceLab/CollectionKit

// apply to the entire CollectionView
collectionView.animator = ScaleAnimator()

// apply to a single section, will override CollectionView's animator
provider.animator = FadeAnimator()

// apply to a single view, will take priority over all other animators
view.collectionAnimator = WobbleAnimator()

您还可以在集合视图中应用其他一些最佳动画 https://github.com/onmyway133/fantastic-ios-animation/blob/master/Animation/layout.md

答案 1 :(得分:0)

这个属于CollectionView中的单元格。您可以通过调用

在单元格中手动设置UIImageView的动画
(url =>{})

您可以找到有关Apple文档或stackoverflow的更多信息。

答案 2 :(得分:0)

我确实用欲望动画创建了一个简单的项目,我没有实现与AppStore完全相同的元素位置,但是我只是向您展示了基本动画以及如何实现它。同样,我也没有检查collectionView何时到达终点,而只是填充了100个元素。

  

我确实添加了GIF,因为它的帧频很低,在模拟器上看起来还不错,没有滞后性

enter image description here

AnimatedCollectionViewController.swift

class AnimatedCollectionViewController: UIViewController {

    @IBOutlet private weak var collectionView: UICollectionView!

    private let identifier = String(describing: AnimatedCollectionViewCell.self)
    private var timer: Timer?

    override func viewDidLoad() {
        super.viewDidLoad()

        /// Loading custom `nib`
        let nib = UINib(nibName: identifier, bundle: Bundle.main)
        collectionView.register(nib, forCellWithReuseIdentifier: identifier)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        /// Starting animation
        startAutoScrollCardsCollectionView()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        /// Remove timer from `RunLoop` object
        timer?.invalidate()
        timer = nil // just in case
    }

    private func startAutoScrollCardsCollectionView() {

        /// This method start timer and fire it immediately
        timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { [unowned self] _ in
            let currentOffset = self.collectionView.contentOffset
            self.collectionView.setContentOffset(CGPoint(x: currentOffset.x + 5, y: currentOffset.y), animated: true)
        }
    }
}

// MARK: - UICollectionViewDataSource extension

extension AnimatedCollectionViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
        return cell
    }
}

// MARK: - UICollectionViewDelegateFlowLayout extension

extension AnimatedCollectionViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 100)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
    }
}

虚拟AnimatedCollectionViewCell.swift

class AnimatedCollectionViewCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

        contentView.backgroundColor = UIColor.random
        contentView.layer.cornerRadius = 20
    }
}

extension CGFloat {
    static func random() -> CGFloat {
        return CGFloat(arc4random()) / CGFloat(UInt32.max)
    }
}

extension UIColor {
    static var random: UIColor {
        return UIColor(red:   .random(),
                       green: .random(),
                       blue:  .random(),
                       alpha: 1.0)
    }
}

所以基本上发生了什么,我开始启动Timer并立即启动它。这个计时器调用completion的时间为0.1秒,在此完成期间,我更改了setContentOffset.x的{​​{1}}位置。

您可以从这里开始,将自定义布局添加到单元格的位置,并检查collectionView何时到达终点。

相关问题