如何在 swift 中将集合视图转换为圆形 icarousel

how to convert a collection view to circular icarousel in swift

我想像在循环列表中那样在集合视图中显示单元格,这意味着在集合视图的 最后一个单元格 之后,滚动集合视图时显示 first cell 又是循环链表

我尝试过使用 icrousel,但由于 icarosuel 只处理视图,我不想完全完成集合视图并重新开始使用 icarousel,所以有什么方法可以让我的集合视图循环

这是我的collectionView代码

        func collectionView(_ collectionView: UICollectionView, 
         cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = 
                collectionView.dequeueReusableCell(withReuseIdentifier: 
                "CellName", for: indexPath as IndexPath) as! CellName

        let gigModel = self.datasoruce?[indexPath.row]

        cell.lblTitle.text = gigModel?.title

        cell.btnPrice.setTitle(gigModel?.getPriceAccordingToGigType(), 
         for: .normal)

          cell.itemImageView.sd_setImage(with: URL(string: 
          (gigModel?.getPhotoPath())!), placeholderImage: 
          UIImage.init(named: "place_holder"))

        cell.itemImageView.layer.cornerRadius = 10.0

        if Utilities.isValidString(object: gigModel?.adminId as 
         AnyObject) {
            cell.btnStar.isHidden = false
        }
        else {
            cell.btnStar.isHidden = true
        }

        return cell
         }

我希望这是一个循环列表。

//使用下面的代码获取下一个单元格。

func scrollToNextCell(){
    //get Collection View Instance
    let collectionView:UICollectionView;

    //get cell size
    let cellSize = CGSizeMake(self.view.frame.width, self.view.frame.height);

    //get current content Offset of the Collection view
    let contentOffset = collectionView.contentOffset;

    //scroll to next cell
    collectionView.scrollRectToVisible(CGRectMake(contentOffset.x + cellSize.width, contentOffset.y, cellSize.width, cellSize.height), animated: true);

}

我尝试创建示例项目,它非常简单,这里是示例代码,您可以如何实现 "infinite" 滚动

    class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var array: [Any] = [0,1,2,3,4,5,6,7,8,9]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return Int(Int16.max) // Int.max cause crash
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCell
        let correctIndex = indexPath.row >= array.count ? indexPath.row % array.count : indexPath.row
        cell.nameLabel.text = "\(array[correctIndex])"
        return cell
    }

}

希望对您有所帮助