ios - 页面控件在完全转到下一页之前切换页面

标签 ios swift uicollectionview uipagecontrol

我为 Collection View 创建了一个页面控件,每个 Collection View 单元格都是屏幕的高度和宽度。 这是页面控件的设置:

// Setup the page control
func setupPageControl() {

    pageControl.pageIndicatorTintColor = cellColor
    pageControl.currentPageIndicatorTintColor = themeColor

    view.addSubview(pageControl)
    addPageControlConstraints()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    pageControl.numberOfPages = collectionViewCellData.count
    return collectionViewCellData.count
}

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

页面控件有个小问题,检查一下:

Screen recording

请注意,当我还没有完全转到下一页时,页面控件是如何变化的,我该如何解决这个问题?

最佳答案

问题很简单,就是您在 willDisplay 单元格中设置了页面控件的 currentPage,关于它的文档说:

Tells the delegate that the specified cell is about to be displayed in the collection view.

这是一个在显示单元格之前调用的方法。

您应该使用 scrollViewDidEndDecelerating(_ scrollView: UIScrollView) 来达到您的目的。

关于ios - 页面控件在完全转到下一页之前切换页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59270115/

相关文章:

ios - 基于 iOS 控件的 FireMonkey 控件行为不正确

swift - 将字典存储在单个数组中

ios - 具有动态和静态单元格的 UICollectionView

Swift:从 Internet 下载图像并缓存它们无法正常工作。需要建议

ios - 快速取消 NSDictionary

ios - 如何使用 MPMediaQuery 检索艺术家列表?

ios - 在 UIPresentationController 中动画呈现 View

ios - 从其他类调用 CollectionView 中的下一张图像

swift - 如何从swift中的字符串索引中获取特定字符

ios - 如何链接两个相互连接的 Collection View ?