禁用collectionView用户交互或在“自定义”单元格类中滚动

时间:2018-07-08 07:47:12

标签: swift uicollectionview

我在根视图中有一个UICollectionView

UICollectionView有一个名为HomeCell的自定义单元格。

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! HomeCell

在HomeCell中,我添加bgView和标题,如下所示:

HomeCell类:

class HomeCell: UICollectionViewCell {

var bgView = UIView()
var title = UILabel()

override init(frame: CGRect) {
    super.init(frame: frame)

    bgView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
    bgView.layer.cornerRadius = 20.0
    bgView.backgroundColor = UIColor.gray

    title.text = "Test"
    title.textAlignment = .center
    title.frame = CGRect(x: 0, y: 210, width: 200, height: 40)
    title.textColor = .black

    let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTapCollapse(_:)))
    bgView.addGestureRecognizer(tap)

    self.addSubview(bgView)
    self.addSubview(title)
}

@objc func handleTapCollapse(_ sender: UITapGestureRecognizer) {

    print("disabled")
    // her I want to disable my collectionView

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
 }

我想要在触摸bgView时禁用UICollectionView滚动。

我在didSelectItemAt中尝试过,但是没有用。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if indexPath.row == 1 {
        animateCamerCell()
    }
}

有可能吗?如果不是,该问题的解决方案是什么?

2 个答案:

答案 0 :(得分:1)

像这样在您的单元格中首先建立协议

protocol touchDelegate:class {
func DidTap(OnView view:UIView,with tap:UITapGestureRecognizer)
}

class HomeCell: UICollectionViewCell {

var bgView = UIView()
var title = UILabel()

weak var delegate:touchDelegate? 

override init(frame: CGRect) {
super.init(frame: frame)

bgView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
bgView.layer.cornerRadius = 20.0
bgView.backgroundColor = UIColor.gray

title.text = "Test"
title.font = UIFont(name: "IRANSansFaNum", size: 18)
title.textAlignment = .center
title.frame = CGRect(x: 0, y: 210, width: 200, height: 40)
title.textColor = .black

let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTapCollapse(_:)))
bgView.addGestureRecognizer(tap)

self.addSubview(bgView)
self.addSubview(title)
}

@objc func handleTapCollapse(_ sender: UITapGestureRecognizer) {

print("disabled")
delegate?.DidTap(OnView: tap.view, with:sender)

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

现在已与您的控制器共同确认协议

class yourcontroller:UIViewController, touchDelegate{
}

现在像在您的数据源方法中进行更改

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! HomeCell
    cell.delegate = self
}

并在您的控制器中实现委托方法

func DidTap(OnView view:UIView,with tap:UITapGestureRecognizer){
   self.yourcollectionviewVariable.isScrollEnabled = false
}

答案 1 :(得分:1)

您无需任何protocol就可以做到这一点。

您可以提取weak个当前UICollectionview的变量,然后在handleTapCollapse function中禁用滚动。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! HomeCell
    cell.collectionView = collectionView
}

HomeCell中:

class HomeCell: UICollectionViewCell {

    weak var collectionView:UICollectionView?

    override init(frame: CGRect) {
        super.init(frame: frame)

    }
    @objc func handleTapCollapse(_ sender: UITapGestureRecognizer) {
        print("disabled")
        collectionView?.isScrollEnabled = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}