[英]Make page control work only for 1 collection view(multiple collectionViews in View Controller)
I have 3-4 collectionViews in a page and one of them has page control which is working as expected.我在一个页面中有 3-4 个 collectionView,其中一个具有按预期工作的页面控件。 The problem is its also working when I scroll through cells of other collection views.
问题是当我滚动浏览其他集合视图的单元格时它也起作用。 So the dots will move regardless of whichever collectionView I'm scrolling through while it should only move for 1st collectionView.
因此,无论我滚动浏览哪个collectionView,这些点都会移动,而它应该只为第一个collectionView 移动。 Here is how I'm enabling paging for 1 collectionView which is probably applying for every collection view:
以下是我为 1 个 collectionView 启用分页的方式,该 collectionView 可能适用于每个集合视图:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Collection View Delegates & Datasource
let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.size.width, height: cvGarages.frame.size.height)
flowLayout.scrollDirection = .horizontal
flowLayout.minimumLineSpacing = 0
collectionView1.collectionViewLayout = flowLayout
collectionView1.isPagingEnabled = true
collectionView1.showsHorizontalScrollIndicator = false
collectionView1.delegate = self
collectionView1.dataSource = self
let flowLayout2 = UICollectionViewFlowLayout()
flowLayout2.itemSize = CGSize(width: 171, height: 232)
flowLayout2.scrollDirection = .horizontal
flowLayout2.minimumLineSpacing = 0
flowLayout2.minimumInteritemSpacing = 0
collectionView2.collectionViewLayout = flowLayout2
collectionView2.contentInset = UIEdgeInsets(top: 0, left: 7, bottom: 0, right: 0)
collectionView2.delegate = self
collectionView2.dataSource = self
//Page Control
pageControl.numberOfPages = 4
}
//ScrollView delegate method
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageWidth = scrollView.frame.width
self.currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
self.pageControl.currentPage = self.currentPage
}
In scrollViewDidScroll()
method check if scrollView
is equal to ( ===
) the specific collectionView
.在
scrollViewDidScroll()
方法中检查scrollView
是否等于( ===
)特定的collectionView
。 ===
operator will check the reference point of same instance. ===
运算符将检查同一实例的参考点。
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView === self.collectionView1 {
let pageWidth = scrollView.frame.width
self.currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
self.pageControl.currentPage = self.currentPage
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.