[英]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.