【问题标题】:Swift -prevent fast scrolling when collectionView.isPagingEnabled = trueSwift - 当 collectionView.isPagingEnabled = true 时防止快速滚动
【发布时间】:2021-03-15 00:18:13
【问题描述】:

我有一个启用分页的 collectionView:

collectionView.isPagingEnabled = true

collectionView 是全屏的(一次只显示 1 个单元格),我有一些在 scrollViewWillEndDragging 中运行的代码。当我正常滚动或半快速滚动时,cellForItemscrollViewWillEndDragging 在同一页面/indexPath.item 上。但是当我快速滚动时,它们都搞砸了,cellForItem 总是在当前页面的后面

**当启用分页时,如何防止用户快速滚动?

var currentItem: Int?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeue...

    currentItem = indexPath.item

    return cell
}

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    let yAxis = targetContentOffset.pointee.y
    let page = Int(yAxis / collectionView.frame.height)

    guard let currentItem = currentItem else { return }
    if page == currentItem {

        // only do something when page and the current indexPath.item are equal which works fine with normal scrolling
    }
}

【问题讨论】:

    标签: ios swift uicollectionview uiscrollview


    【解决方案1】:

    这个answer 工作:

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    
        scrollView.isUserInteractionEnabled = false
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    
        scrollView.isUserInteractionEnabled = true
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 2013-05-16
      • 2020-09-09
      相关资源
      最近更新 更多