我在一個頁面中有 3-4 個 collectionView,其中一個具有按預期作業的頁面控制元件。問題是當我滾動瀏覽其他集合視圖的單元格時它也起作用。因此,無論我滾動瀏覽哪個collectionView,這些點都會移動,而它應該只為第一個collectionView 移動。以下是我為 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
}
uj5u.com熱心網友回復:
在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
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/472720.html
標籤:迅速 代码 uicollectionview uipagecontrol
