- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 CollectionView,其中有 2 个项目。
CollectionView
有 pagingEnabled
,我想循环显示它们,如下所示:
[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/
我在 Storyboard 中有一个 collectionView,如下所示: 注意约束:Collection View.top = Search Bar.bottom 我不想要该行上方的 Colle
我有一个包含两个部分的 Collection View ,每个部分都有一个项目。部分中的单元格包含 Collection View ,我需要使高度单元格适合内容 Collection View 。 我
我见过将 Collection View 嵌套在 TableView 中的解决方案,但对于我的应用程序,我需要有 2 个 Collection View ,因为这样可以更轻松地执行其他操作。 因此,让
感谢帮助。 如上所示,CollectionView ItemSize.height 比 CollectionView.height 大。我只是在 viewDidLayoutSubviews() 中设置
我有一个collectionview(A),它可以作为“卡片 View ”来滑动。在最后一个卡/单元格中,我有另一个 Collection View (B)来显示更多单元格。到目前为止,一切都很好!一
我正在寻找一种方法,当您滚动到 collectionView 中的另一个单元格时,可以更改 imageView 中的图像。 因此,当滚动单元格的 (1) 时,我想更改图像 (2)。 创建此内容的最佳方
我的 NSCollectionView 项目集成了另一个 NSCollectionView。一切都很好,但在第一个 View 中选择项目时就不行了。 当我设置collectionView1.isSel
我是一名 Android 开发者,正在尝试学习 iO 开发。 我正在尝试在水平分页滚动器内实现垂直滚动 UI(就像 ViewPager 内的 RecyclerView)。 我知道我应该使用嵌套的 UI
我在collectionView中有一个collectionView,我希望能够选择第二个collectionView中的一个单元格来执行到另一个ViewController的segue。 目前,当我
我有这两个代表: #pragma mark = UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView*)co
嘿,我正在尝试在我的动态 Collection View 单元格之后添加一个静态 Collection View 单元格。例如在我的屏幕截图中,在“Study Grind Edition” Colle
是否可以通过在 Collection View Cell .swift 文件中编写代码来阻止我的 Collection View 滚动。我希望能够在用户点击单元格中的按钮时停止滚动,然后在再次按下按钮
我有一个用例,我想要呈现一组collectionViews(或复合 View )。使用 marionette.js 执行此操作的最佳方法是什么? 我的模型结构如下- A | |-- Collectio
我有一个 View Controller ,其中有 tableView 和 Collection View 。我为 collectionView 和 tableView 设置了相同的高度。 前半部分是
在 Visual Studio 2017 中,尝试存档 Xamarin Android 项目时,成功构建后出现以下错误: 无法创建应用程序存档“MyArchive”。这种类型的 CollectionV
我对 Collection View 完全陌生,请帮助我在 Collection View 中垂直和水平滚动单元格: viewcontroller.h #import @interface View
我在一个表格 View 单元格中有两个不同的 collectionView。原因是因为我试图找到实现一个水平 collectionView 和一个显示不同数据的垂直 collecitonView 的最
我想在另一个 collectionview 标题中实现一个 Collection View 幻灯片,但是当我尝试连接 uicollectionview 的导出时,我收到一条错误消息,指出导出无法连接到
您好,我已经为这个问题苦苦挣扎了一段时间,一直想不出来。 我想做的是,在 collectionview 单元格内按下一个按钮,然后执行到下一个单元格的转换。 这是我想在 collectionview
在 collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayo
我是一名优秀的程序员,十分优秀!