使页面控制仅适用于 1 个集合视图(视图控制器中的多个集合视图)

Make page control work only for 1 collection view(multiple collectionViews in View Controller)

我在一个页面中有 3-4 个 collectionView,其中一个有页面控件,它按预期工作。问题是当我滚动浏览其他集合视图的单元格时它也起作用。因此,无论我滚动浏览哪个 collectionView,这些点都会移动,而它应该只为第一个 collectionView 移动。以下是我如何为 1 个 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
}

scrollViewDidScroll() 方法中检查 scrollView 是否等于 (===) 特定的 collectionView=== 运算符将检查同一实例的参考点。

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
    }
}