1
是否有可能通过在集合视图Cell .swift文件中编写代码来停止滚动我的集合视图。我希望能够在用户点击单元格中的按钮时停止滚动,然后再次按下按钮时允许滚动。控制collectionView从collectionView中滚动单元格
是否有可能通过在集合视图Cell .swift文件中编写代码来停止滚动我的集合视图。我希望能够在用户点击单元格中的按钮时停止滚动,然后再次按下按钮时允许滚动。控制collectionView从collectionView中滚动单元格
为你的创建自定义的委托
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
}
}
快乐编码代表!
太棒了。这是什么意思,如果button.tag等于1? – Honey
代码将切换isScrollEnabled的值。也就是说,如果滚动,如果启用,按钮点击将禁用它,反之亦然。 –
我在问一些不同的东西。纠正我,如果我错了:默认情况下,按钮标记设置为'0'(我的意思是你不需要给它一个初始值,右)...所以一旦它被点击,你将它设置为'1'并根据你的决定做出“isScrollEnabled”......你基本上在滥用标签属性来做这个操作...... – Honey