我在根视图中有一个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()
}
}
有可能吗?如果不是,该问题的解决方案是什么?
答案 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")
}
}