【问题标题】:Disable swipe in one direction for UICollectionView禁用 UICollectionView 的单向滑动
【发布时间】:2017-03-03 09:07:05
【问题描述】:

我在一个单元格中有一个 collectionView。 collectionView 在水平方向滑动。

在collectionView中我调用了这个函数:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offset = scrollView.contentOffset.x
    print(offset)
}

当偏移量为 0 时,用户无需向左滑动,因为在该方向上看不到任何东西。

当偏移量为 0 时,如何暂时禁用单向滚动?在这种情况下,在向右滚动时禁用向左滚动仍然处于启用状态。

编辑:我忘了说,带有 collectionView 的单元格位于 collectionView 的另一个单元格内,这两个单元格都水平滚动。我想在子单元格上禁用滚动并同时在父单元格上启用滚动。所以,当子单元格内的collectionView水平滚动不再启用时,父单元格开始滚动。

class ViewController: UIViewController {

lazy var collectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.delegate = self
    cv.dataSource = self
    cv.isPagingEnabled = true
    return cv
}()

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.item == 0 {
                    // Other cell here to swipe to when user cant swipe collectionView inside child cell.
    } else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ParentCell.reuseIdentifier, for: indexPath) as! ParentCell
        return cell
    }
}

}

父单元:

class ParentCell: UICollectionViewCell {

lazy var collectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.delegate = self
    cv.dataSource = self
    cv.isPagingEnabled = true
    cv.showsHorizontalScrollIndicator = false
    return cv
}()


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ChildCell.reuseIdentifier, for: indexPath) as! ChildCell
    return cell

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: view.frame.height)
}


}

子细胞:

class ChildCell: UICollectionViewCell {

lazy var collectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.delegate = self
    cv.dataSource = self
    cv.tag = 1
    return cv
}()


func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offset = scrollView.contentOffset.x
    print(offset)
}
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    您可以使用以下命令强制偏移:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == myNotScrollingView {
            //you will never bounce to left
            scrollView.contentOffset.x = max(0,scrollView.contentOffset.x)
        }
    }
    

    【讨论】:

      【解决方案2】:

      喜欢

       // create one global float value for get the every scroll
       var previousX: Float = 0.0
      

      为你的父子collectionview设置tag,然后根据tag访问

      func scrollViewDidScroll(_ scrollView: UIScrollView) {
      
      if scrollView.contentOffset.x < previousX {
            if  scrollView.tag == 1 // set your tag
          {
          scrollView.contentOffset = CGPoint(x: CGFloat(previousX), y: CGFloat(scrollView.contentOffset.y))
      }
      else {
          previousX = scrollView.contentOffset.x
      }
          }
      }
      

      更新

         func scrollViewDidScroll(_ scrollView: UIScrollView) {
      
       if scrollView.contentOffset.x < previousX {
      
            var cell: UICollectionViewCell? = (UICollectionView.viewwithtag(1) as? UICollectionViewCell)
      
      
      
            if  cell.tag == 1 // set your tag
          {
          scrollView.contentOffset = CGPoint(x: CGFloat(previousX), y: CGFloat(scrollView.contentOffset.y))
      }
      else {
          previousX = scrollView.contentOffset.x
      }
          }
      }
      

      更新 2

         func scrollViewDidScroll(_ scrollView: UIScrollView) {
      
       if scrollView.contentOffset.x < previousX {
      
            var cell: UICollectionViewCell? = (UICollectionView.viewwithtag(1) as? UICollectionViewCell)
      
      
      
            if (cell is ChildCell)
          {
          scrollView.contentOffset = CGPoint(x: CGFloat(previousX), y: CGFloat(scrollView.contentOffset.y))
      }
      else {
          previousX = scrollView.contentOffset.x
      }
          }
      }
      else if (cell is ParentCell) {
      //Another custom cell
       }
       else {
      //General cell
      }
      
      }
      

      【讨论】:

      • 我对标签的工作原理感到困惑。如果我在子单元格内设置collectionView.tag = 1,结果仍然与我无法向左滚动但父视图不滚动的位置相同。
      • @luke - 你能多加点代码吗,很容易解决
      • @luke - 我还添加了另一个选项
      • @luke - 你理解获取标签的逻辑
      猜你喜欢
      • 2019-04-07
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2017-05-02
      • 1970-01-01
      相关资源
      最近更新 更多