Ios 轻触后启用分页的UICollectionView滚动问题

Ios 轻触后启用分页的UICollectionView滚动问题,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我有一个collectionView,当我点击第二页的新单元格时,它会返回到第一个单元格 虽然它应该是第二页,但我在第二页得到了0.0,在第一页也得到了0.0 func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { let pageWidth = collectionCustom.frame.size.width let page = floor((collectionCustom.cont

我有一个
collectionView
,当我点击第二页的新单元格时,它会返回到第一个单元格

虽然它应该是第二页,但我在第二页得到了0.0,在第一页也得到了0.0

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

        let pageWidth = collectionCustom.frame.size.width
        let page = floor((collectionCustom.contentOffset.x - pageWidth / 2) / pageWidth) + 1

        print("Page number: \(page)")

    }
我在
didSelectItem
方法中没有发生任何事情,那么为什么我会得到那个滚动条呢

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: (inout CGPoint)) {

        let pageWidth: Float = Float(collectionCustom.frame.width)
        // width + space
        let currentOffset: Float = Float(collectionCustom.contentOffset.x)
        let targetOffset: Float = Float(targetContentOffset.x)
        var newTargetOffset: Float = 0
        if targetOffset > currentOffset {
            newTargetOffset = ceilf(currentOffset / pageWidth) * pageWidth
        }
        else {
            newTargetOffset = floorf(currentOffset / pageWidth) * pageWidth
        }
        if newTargetOffset < 0 {
            newTargetOffset = 0
        }
        else if newTargetOffset > Float(collectionCustom.contentSize.width) {
            newTargetOffset = Float(collectionCustom.contentSize.width)
        }

        targetContentOffset.x = CGFloat(currentOffset)
        collectionCustom.setContentOffset(CGPoint(x: CGFloat(newTargetOffset), y: CGFloat(0)), animated: true)

        var index: Int = Int(newTargetOffset / pageWidth)

        var cell: UICollectionViewCell? = collectionCustom.cellForItem(at: IndexPath(item: index, section: 0))

        cell = collectionCustom.cellForItem(at: IndexPath(item: index + 1, section: 0))

        }

    }
func-scrollView-willenddragging(scrollView:UIScrollView,带速度:CGPoint,targetContentOffset:(inout-CGPoint)){
let pageWidth:Float=Float(collectionCustom.frame.width)
//宽度+空间
让currentOffset:Float=Float(collectionCustom.contentOffset.x)
让targetOffset:Float=Float(targetContentOffset.x)
var NEWTAGETOFSET:Float=0
如果targetOffset>currentOffset{
newTargetOffset=ceilf(当前偏移量/页面宽度)*页面宽度
}
否则{
newTargetOffset=地板(当前偏移量/页面宽度)*页面宽度
}
如果newTargetToffset<0{
newTargetToffset=0
}
如果newTargetOffset>Float(collectionCustom.contentSize.width),则为else{
newTargetOffset=Float(collectionCustom.contentSize.width)
}
targetContentOffset.x=CGFloat(currentOffset)
collectionCustom.setContentOffset(CGPoint(x:CGFloat(newTargetOffset),y:CGFloat(0)),动画:true)
变量索引:Int=Int(newTargetOffset/pageWidth)
变量单元格:UICollectionViewCell?=collectionCustom.cellForItem(位于:IndexPath(项:索引,节:0))
cell=collectionCustom.cellForItem(位于:IndexPath(项:索引+1,节:0))
}
}

如果您想查看当前页面,那么您应该从scrollview获取页面,如下所示

 func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let pageNumber = round(scrollView.contentOffset.x / collectionCustom.frame.width) // You can change width according you 
    pageControl.currentPage = Int(pageNumber)

}

您能否添加更多关于您正在经历的行为和所需行为的描述?您在问题中输入的代码中没有任何内容会导致问题。您需要包含更多的代码,或者尝试自己调试它并添加更多的调试信息。我希望
collectionView
每页显示4个单元格,这是正确的,但是当我有5到7个单元格时,它将不会显示日志中的下一页,这就是为什么我会滚动回第一个单元格。它适用于8个对象,同样适用于4的倍数。理想情况下,如果我有7个对象,我希望看到3个单元格,以此类推,但它一直在显示4个。@Christian Welcome mate