Swift 在自定义单元格类中禁用collectionView用户交互或滚动
我在根视图中有一个Swift 在自定义单元格类中禁用collectionView用户交互或滚动,swift,uicollectionview,Swift,Uicollectionview,我在根视图中有一个UICollectionView UICollectionView有一个名为HomeCell的自定义单元格 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! HomeCell 在HomeCell中,我添加了如下视图和标题: HomeCell类: class HomeCell: UICollectionViewCe
UICollectionView
UICollectionView
有一个名为HomeCell
的自定义单元格
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! HomeCell
在HomeCell中,我添加了如下视图和标题:
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
滚动
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! HomeCell
cell.collectionView = collectionView
}
我在中尝试了didSelectItemAt
,但没有成功
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 1 {
animateCamerCell()
}
}
可能吗?如果没有,这个问题的解决方案是什么 首先在您的单元格中创建如下协议
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
}
首先像这样在你的细胞里制定协议
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
}
您可以在不使用任何
协议的情况下执行此操作
您可以使用当前UICollectionview
的weak
变量,并在handleapcollapse函数中禁用滚动
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")
}
}
您可以在不使用任何协议的情况下执行此操作
您可以使用当前UICollectionview
的weak
变量,并在handleapcollapse函数中禁用滚动
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")
}
}
是的,有可能collectionview.isScrollEnabled=false
@RahulGUsai不在func handleTapCollapse中工作。您可以添加协议以将触摸事件传输到控制器。有可能collectionview.isScrollEnabled=false
@RahulGUsai不在func handleTapCollapse中工作。您可以添加协议以传输触摸事件到控制器