gpt4 book ai didi

ios - CollectionView循环显示项目分页

转载 作者:行者123 更新时间:2023-11-30 12:46:04 25 4
gpt4 key购买 nike

我有一个 CollectionView,其中有 2 个项目。

CollectionViewpagingEnabled,我想循环显示它们,如下所示:

[1] 2 -> [2] 1 -> [1] 2 -> [2] 1

是否可以使用CollectionView来做到这一点

我的代码:

extension HomeViewController: UICollectionViewDelegateFlowLayout {
func configureHeaderView() {

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

headerView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: headerHeight), collectionViewLayout: layout)
headerView?.backgroundColor = .blue
headerView?.isPagingEnabled = true
headerView?.isUserInteractionEnabled = true

headerView?.rx.setDelegate(self).addDisposableTo(disposeBag)
let nib = UINib(nibName: BannerCollectionViewCell.reuseIdentifier, bundle: nil)
headerView?.register(nib, forCellWithReuseIdentifier: BannerCollectionViewCell.reuseIdentifier)
headerView?.showsHorizontalScrollIndicator = false

tableView.tableHeaderView = headerView

if let headerView = headerView {
self.bannerItems.asObservable().bindTo(headerView.rx.items(cellIdentifier: BannerCollectionViewCell.reuseIdentifier, cellType: BannerCollectionViewCell.self)) {
row, banner, cell in
cell.banner = banner
}.addDisposableTo(disposeBag)
}
}

// MARK: UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: headerHeight)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}

最佳答案

如果 collectionView 到达末尾且用户向右滑动,则利用 UICollectionView 的 scrollToItemAtIndexPath 方法将 collectionView 移回第一项。如果用户从第一项向左滑动,则将 collectionView 移回到最后一项。您应该将animation属性设置为false

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {

// Calculate where the collection view should be at the right-hand end item
let offsetRight = collectionView.frame.size.width * CGFloat(yourDataArray.count)

if scrollView.contentOffset.x == offsetRight {

// user is scrolling to the right from the last item to the 'fake' item 1.
// reposition offset to show the 'real' item 1 at the left-hand end of the collection view

let newIndexPath = IndexPath(row: 1, section: 0)

collectionView.scrollToItem(at: newIndexPath, at: .left, animated: false)

} else if scrollView.contentOffset.x == 0 {

// user is scrolling to the left from the first item to the fake 'item N'.
// reposition offset to show the 'real' item N at the right end of the collection view

let newIndexPath = IndexPath(row: (yourDataArray.count-2), section: 0)

collectionView.scrollToItem(at: newIndexPath, at: .left, animated: false)
}
}

关于ios - CollectionView循环显示项目分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41632920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com