简体   繁体   English

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

[英]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.

相关问题 一个View Controller上有两个CollectionViews - Two CollectionViews on one View Controller 同一视图控制器中的水平和垂直集合视图 - Horizontal and vertical collectionVIews in the same view Controller 滚动视图,集合视图和一页控件 - scroll view, collection view and one page control 使视图控制器可与集合视图一起滚动 - make view controller scrollable with collection view 在 3 个不同的 CollectionViews 中选择特定的单元格以对特定的视图控制器执行 Segue - Selecting Specific Cells in 3 different CollectionViews to perform a Segue to a specific View Controller 在具有多个集合视图的视图 Controller 中的特定集合视图上应用 UICollectionViewDelegateFlowLayout - Apply UICollectionViewDelegateFlowLayout on specific Collection View in View Controller with multiple Collection Views iOS-对视图中的多个Tableview或CollectionView使用/传递手势识别器(Swift) - iOS - Use/pass gesture recognizer for multiple Tableviews or Collectionviews in a View(Swift) 不同控件的页面控件或滚动视图 - Page control or scroll view for different controller Swift 3-仅在视图控制器内部隔离集合视图的界面 - Swift 3 - Interface That Only Segues A Collection View Inside Of A View Controller 从另一个View Controller访问页面控件以转发页面 - Access to page control from another View Controller to forward the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM