如何在UICollectionView图像滚动中添加PageControl

Swi*_*per 27 uipagecontrol ios uicollectionview swift swift3

你好我有UICollectionView水平图像列表代码,我想PageControl在滚动图像显示时添加,我添加了pagecontrol选择器,IBOutlet但是,我如何整合它?之间UICollecitonView.?

我的代码如下.

 class  ViewController: UIViewController,  UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {


    @IBOutlet weak var View : DesignableView!


    @IBOutlet var collectionView: UICollectionView!
    @IBOutlet var collectionViewLayout: UICollectionViewFlowLayout!

    @IBOutlet open weak var pageControl: UIPageControl? {
        didSet {
            pageControl?.addTarget(self, action: #selector(ViewController.pageChanged(_:)), for: .valueChanged)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

         pageControl?.numberOfPages = 11


    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 11;
    }




    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: ImageCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell
        cell.label.text = "Cell \(indexPath.row)"
        cell.backgroundImageView.image = UIImage(named: "Parallax \(indexPath.row + 1)")

        // Parallax cell setup
        cell.parallaxTheImageViewScrollOffset(self.collectionView.contentOffset, scrollDirection: self.collectionViewLayout.scrollDirection)
        return cell
    }



    @IBAction open func pageChanged(_ sender: UIPageControl) {

        print(sender.currentPage)


    }




    // MARK: Delegate

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.bounds.size;
    }

    // MARK: Scrolling

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // Parallax visible cells
        for cell: ImageCollectionViewCell in collectionView.visibleCells as! [ImageCollectionViewCell] {
            cell.parallaxTheImageViewScrollOffset(self.collectionView.contentOffset, scrollDirection: self.collectionViewLayout.scrollDirection)
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}
Run Code Online (Sandbox Code Playgroud)

luk*_*uke 92

我不是接受的答案的粉丝.有时,willDisplay cell将根据用户交互返回页面控件的错误页面.

我用的是什么:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    pageControl.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}
Run Code Online (Sandbox Code Playgroud)

用户完成滚动从而使后这将更新页面currentPagepageControl更准确.


Jua*_*rti 43

在这里,您可以在水平集合视图中使用页面控制分页的完整类.这正在我的一个应用程序上工作,并且正在正常工作.如果你不能让它工作,请随时问我,我会帮助你.

首先,在Storyboard中,您必须设置CollectionView:

布局:流程
滚动方向:
启用 水平滚动启用
分页

@IBOutlet weak var collectionView: UICollectionView! //img: 77

@IBOutlet weak var pageControl: UIPageControl!

var thisWidth:CGFloat = 0

override func awakeFromNib() {
    super.awakeFromNib()

    thisWidth = CGFloat(self.frame.width)
    collectionView.delegate = self
    collectionView.dataSource = self

    pageControl.hidesForSinglePage = true

}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 10
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCell", for: indexPath)

    return cell
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    self.pageControl.currentPage = indexPath.section
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    thisWidth = CGFloat(self.frame.width)
    return CGSize(width: thisWidth, height: self.frame.height)
}
Run Code Online (Sandbox Code Playgroud)

  • 那是因为为了为每个项目制作一个页面,您必须使用部分,而不是行 (2认同)
  • 但是使用此解决方案时,当用户滑动到下一页,然后在不松开手指的情况下再次向后滑动时,pageControl将显示错误的页面。我认为您应该考虑@luke的答案 (2认同)

Dav*_*aez 28

对于响应更快的UIPageControl,我更喜欢使用scrollViewDidScroll和计算关于scrollView水平中心的偏移量.

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offSet = scrollView.contentOffset.x
    let width = scrollView.frame.width
    let horizontalCenter = width / 2

    pageControl.currentPage = Int(offSet + horizontalCenter) / Int(width)
}
Run Code Online (Sandbox Code Playgroud)

  • 最好的解决方案!响应准确 (2认同)

Chr*_*ute 11

等待scrollViewDidEndDecelerating结果缓慢更新UIPageControl.如果你想要一个响应更快的用户界面,我建议改为实施scrollViewWillEndDragging(_:withVelocity:targetContentOffset:).

Swift 4示例,其中每个集合视图部分是一个页面:

override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if let collectionView = scrollView as? ScoreCollectionView,
        let section = collectionView.indexPathForItem(at: targetContentOffset.pointee)?.section {
        self.pageControl.currentPage = section
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果刷得足够快,当前页面可能会产生错误的结果. (2认同)

小智 8

快速 5 示例:

根据 CollectionView 的索引设置 pageController CurrentPage 的函数:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
   let visibleRect = CGRect(origin: self.collectionView.contentOffset, size: self.collectionView.bounds.size)
   let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
   if let visibleIndexPath = self.collectionView.indexPathForItem(at: visiblePoint) {
            self.pageController.currentPage = visibleIndexPath.row
   }
}
Run Code Online (Sandbox Code Playgroud)

PageControllerAction 函数在选择 PageController 时滚动 CollectionView :

@IBAction func pageControllerAction(_ sender: UIPageControl) {
       self.collectionView.scrollToItem(at: IndexPath(row: sender.currentPage, section: 0), at: .centeredHorizontally, animated: true)
}
Run Code Online (Sandbox Code Playgroud)


Sha*_*med 6

Swift 5非常流畅的工作

//MARK:- For Display the page number in page controll of collection view Cell
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let visibleRect = CGRect(origin: self.collectionview.contentOffset, size: self.collectionview.bounds.size)
    let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
    if let visibleIndexPath = self.collectionview.indexPathForItem(at: visiblePoint) {
        self.pageControl.currentPage = visibleIndexPath.row
    }
}
Run Code Online (Sandbox Code Playgroud)


lba*_*osa 5

卢克(Luke)的回答对我来说是一个不错的开始,但是有两个问题:它不经常更新(已经在其他答案中指出),但最重要的是,它没有为我的收藏夹视图中的最后一个项目显示正确的页面(面向水平)并且仅当该项目几乎要离开屏幕时才切换页面。

正如其他人所指出的那样,对于第一个问题,使用scrollViewDidScroll提供了更好的体验。我担心拨打电话如此频繁而导致的性能问题,但没有发现任何问题。

至于第二个问题,对于我的用例,找到位于屏幕中心的单元格的indexPath提供了更好的解决方案。请记住,如果您的用例可以容纳屏幕上两个以上的单元,则可能需要进行调整。这是因为您的最后一个单元格永远不会到达屏幕的中间。我会争辩说,在那种情况下使用UIPageControl反正可能并不理想。

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let visibleRect = CGRect(origin: self.collectionView.contentOffset, size: self.collectionView.bounds.size)
    let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
    if let visibleIndexPath = self.collectionView.indexPathForItem(at: visiblePoint) {
        self.pageControl.currentPage = visibleIndexPath.row
    }
}
Run Code Online (Sandbox Code Playgroud)