正确的 scrollViewWillEndDragging 代码?

correct scrollViewWillEndDragging code?

如何让 UIScrollView 在视图的末尾停止拖动,并将其对齐到正确的位置?我曾尝试使用以下代码,但虽然它正确减速并捕捉到视图顶部的完美位置,但它不允许我向下滚动:

 func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

        let layout = self.collectionView?.collectionViewLayout as! UICollectionViewFlowLayout

        let itemHeightIncludingSpacing = self.view.frame.height + layout.minimumInteritemSpacing

        var offset = targetContentOffset.memory

        let index = (offset.x + scrollView.contentInset.bottom) / itemHeightIncludingSpacing
        let roundedIndex = round(index)

        offset = CGPoint(x: roundedIndex * itemHeightIncludingSpacing - scrollView.contentInset.bottom, y: -scrollView.contentInset.top)
        targetContentOffset.memory = offset
    }

这是我的滚动视图的顶部:

这是底部,它不在您看到的应该在的位置。它应该与底部偏移值齐平,就像顶部一样:

如何让滚动条像顶部一样停止刷新?

您是否对具有该方法的 UIScrollView 进行了扩展?它不存在,但 scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) 存在。如果这是您在扩展中的自定义方法,请确保它在您的代码中的某处被调用(就像我提到的委托方法)。此外,重置参数对 scrollView 没有任何作用。我建议采用不同的方法。

我也不确定你在这里问什么。您是否不希望内容在弹回原位之前被拉过屏幕边缘? 如果是这样,则不需要该方法。只需将 self.myScrollView.bounces = false 添加到 viewDidLoad()

编辑: 好的,试试这个
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 0, 0)
}