簡體   English   中英

控件collectionView從collectionView Cell中滾動

[英]Control collectionView Scrolling from within collectionView Cell

是否可以通過在集合視圖Cell .swift文件中編寫代碼來阻止我的集合視圖滾動。 我希望能夠在用戶點擊單元格中的按鈕時停止滾動,然后在再次按下按鈕時允許滾動。

為您的單元格創建自定義委托

protocol CustomCellDelegate: class {
    func cellDidSetScrolling(enabled: Bool)
}

class CustomCell: UICollectionViewCell {

    var delegate: CustomCellDelegate?

    // ....
}

將委托分配給cellForItem單元格

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    // dequeue cell and assign delegate
    var cell: CustomCell?
   cell.delegate = self
   return cell
}

按鈕動作調用單元格委托。 使用button.tag確定enabled

func buttonAction() {
    button.tag = button.tag == 0 ? 1 : 0 // toggle value
    delegate?.cellDidSetScrolling(enabled: button.tag == 1)
}

ViewController實現委托

class ViewController: UIViewController, CustomCellDelegate {

    func cellDidSetScrolling(enabled: Bool) {
        collectionView.isScrollEnabled = enabled
    }
}

快樂的編碼!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM