Ios 如何将UIPageControl连接到UICollectionView(Swift)

Ios 如何将UIPageControl连接到UICollectionView(Swift),ios,swift,uicollectionview,uipagecontrol,Ios,Swift,Uicollectionview,Uipagecontrol,我目前正在使用UICollectionView显示3个图像(每个图像跨越整个单元格)。我还有一个UIPageControl,它位于UICollectionView的顶部。我想让UIPageControl显示图像的数量(在本例中为3),以及用户当前正在查看的图像。我想要达到的效果是Instagram应用程序 我目前用于实现此效果的方法是将UIPageControl的更新放在UICollectionView的willDisplay函数中,如下所示: func collectionView(_ col

我目前正在使用UICollectionView显示3个图像(每个图像跨越整个单元格)。我还有一个UIPageControl,它位于UICollectionView的顶部。我想让UIPageControl显示图像的数量(在本例中为3),以及用户当前正在查看的图像。我想要达到的效果是Instagram应用程序

我目前用于实现此效果的方法是将UIPageControl的更新放在UICollectionView的
willDisplay
函数中,如下所示:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    pictureDots.currentPage = indexPath.item
}
这样可以在集合视图和页面控件之间正确连接分页效果。然而,我遇到的问题是UIPageControl一开始就说用户在第三个图像上,即使它正在显示第一个图像


有人知道为什么会发生这种情况以及如何解决此问题吗?

首先使用
UICollectionView
UIPageControl
添加到故事板中,然后将它们作为插座连接到视图控制器

@IBOutlet var pageControl: UIPageControl!
@IBOutlet var collectionView: UICollectionView!
调整
UICollectionViewDataSource
中的
numberOfItemsInSection
方法,将页面控件的计数设置为始终等于集合视图中的单元格数

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    let count = ...

    pageControl.numberOfPages = count
    pageControl.isHidden = !(count > 1)

    return count
}
最后,使用
UIScrollViewDelegate
,我们可以知道
UICollectionView
在哪个单元格上停止。如果未使用
UICollectionViewController
,则可能需要添加代理协议

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    pageControl?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {

    pageControl?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}

这是可能的,因为
UICollectionView
实际上是引擎盖下的
UIScrollView

步骤1为集合视图和页面控制创建变量

@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet var pageControl:UIPageControl!     
override func viewDidLoad() {
    super.viewDidLoad()         
    self.pageControl.numberOfPages = procedures.count
    //Set the delegate
    self.collectionView.delegate = self
}
步骤2设置页面控件的页数

@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet var pageControl:UIPageControl!     
override func viewDidLoad() {
    super.viewDidLoad()         
    self.pageControl.numberOfPages = procedures.count
    //Set the delegate
    self.collectionView.delegate = self
}
*scrollViewDidScroll函数中的步骤3计算当前页面的收集单元格宽度和索引

    extension YourCollectionVC: UICollectionViewDataSource, UICollectionViewDelegate {
            override func scrollViewDidScroll(_ scrollView: UIScrollView) {
                let witdh = scrollView.frame.width - (scrollView.contentInset.left*2)
                let index = scrollView.contentOffset.x / witdh
                let roundedIndex = round(index)
                self.pageControl?.currentPage = Int(roundedIndex)
            }
}
注意:这适用于水平显示的集合视图,对于垂直方向,请更改方法


在swift 4中测试。

使用此功能可实现平稳运行

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let witdh = scrollView.frame.width - (scrollView.contentInset.left*2)
    let index = scrollView.contentOffset.x / witdh
    let roundedIndex = round(index)
    self.pageControl.currentPage = Int(roundedIndex)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    pageControl.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {

    pageControl.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}

当可以有n个页数时,如何确定页数?@Hemang页数应等于集合视图中的项目数。在
numberofitemsinssection
中,通常返回数据源中的元素数。这里截取的数字是
count
,设置了页数。在我的例子中,我有一个固定大小的集合视图,在一列中显示5行。假设有20条记录,它将显示4列,每列5行。所以我需要的是得到4个运行时。如果集合视图在TableView单元格中,这会起作用吗?集合视图数据源和委托是保存TableView而不是TableViewcell类的ViewController类