如何在CollectionView上滑动最后一个单元格的同时滚动到第一个单元格?

时间:2018-12-29 11:02:36

标签: ios swift uicollectionview uicollectionviewcell collectionview

我正在使用其中包含许多单元格的CollectionView。我想做的是:

  • 当用户位于collectionView的最后一个单元格上时,并且当用户滑动以查看下一个单元格时,我希望此collectionView从头开始重新启动。 (基本上,我需要一个无休止的循环)

希望我能正确解释。等待您的解决方案。谢谢。

1 个答案:

答案 0 :(得分:0)

尝试一下。

import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate=self;
        collectionView.dataSource=self;
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
        cell.backgroundColor = UIColor.red
        if indexpath.row==99{
            self.btnScroll()
           }
        return cell
    }

    func btnScroll() {
        collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: UICollectionView.ScrollPosition.top, animated:true)
    }
}
相关问题