将 UILongPressGesture 添加到 Cell 而不是 CollectionView
Adding UILongPressGesture to Cell not CollectionView
我正在尝试将 UILongGestureRecognizer 添加到我的 Custom CollectionView Cell 但 处理函数 从来没有叫。这是我来自自定义单元格的 gesture
和 handlerFunc
:
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
@objc func handleLongPress(_ recognizer: UILongPressGestureRecognizer) {
switch recognizer.state {
case .possible:
break
case .began:
print("began")
break
case .changed:
break
case .ended:
print("ended")
break
case .cancelled:
break
case .failed:
break
@unknown default:
break
}
}
此外,这是我的 cell configure
功能:
func configure(with viewModel: RecordsCollectionViewCellViewModel, itemCount: Int) {
longPress.numberOfTapsRequired = 1
longPress.minimumPressDuration = 0.3
longPress.delaysTouchesBegan = true
longPress.delegate = self
self.bringSubviewToFront(gestureView)
gestureView.isUserInteractionEnabled = true
gestureView.addGestureRecognizer(longPress)
}
gestureView
是单元格顶部的透明视图。
我过去尝试过这个但遇到了问题,我相信与实际 collectionView 上的 contextMenu 交互有关。
您可以通过将手势处理程序放在 collectionView 本身上并获取手势位置来修复它,然后根据此 link;
定位单元格
如果您正在寻找最佳解决方案,我认为有更好的解决方案,例如将 UILongPressGestureRecognizer
添加到集合视图并确定哪个单元格被点击,或者您是否需要在 long 上显示一些选项点击 func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath
并说明推荐它们的原因,因为集合视图上已经有一些手势管理。
但是,如果您已经考虑了所有这些并且仍然觉得您的解决方案适合您,我认为问题出在这一行 let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
上,它似乎与单元格一起默认初始化.
我觉得也许当单元格被重用时,func configure
中的某些内容配置不正确
我会做这些改变:
func configure(with viewModel: RecordsCollectionViewCellViewModel, itemCount: Int) {
// Remove any previous gesture recognizers, maybe this is not needed
gestureView.gestureRecognizers?.removeAll()
// Initialize a new gesture recognizer
let longPress = UILongPressGestureRecognizer(target: self,
action: #selector(handleLongPress))
// longPress.numberOfTapsRequired = 1 - not needed
longPress.minimumPressDuration = 0.3
// longPress.delaysTouchesBegan = true - not needed
// longPress.delegate = self - not needed unless you are doing something else
gestureView.isUserInteractionEnabled = true
gestureView.addGestureRecognizer(longPress)
}
试一试,看看你的函数 handleLongPress
是否被调用了?就我而言,这是可行的。
只在viewDidLoad()中调用setLongPress()
func setLongPress(){
let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.CollectionView.addGestureRecognizer(lpgr)
}
@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer?) {
if gestureRecognizer?.state != .ended {
return
}
let p = gestureRecognizer?.location(in: projectCollectionView)
let indexPath = CollectionView.indexPathForItem(at: p ?? CGPoint.zero)
if indexPath == nil {
print("couldn't find index path")
} else {
// get the cell at indexPath (the one you long pressed)
var cell: UICollectionViewCell? = nil
if let indexPath = indexPath {
// your action here
}
// do stuff with the cell
}
}
我正在尝试将 UILongGestureRecognizer 添加到我的 Custom CollectionView Cell 但 处理函数 从来没有叫。这是我来自自定义单元格的 gesture
和 handlerFunc
:
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
@objc func handleLongPress(_ recognizer: UILongPressGestureRecognizer) {
switch recognizer.state {
case .possible:
break
case .began:
print("began")
break
case .changed:
break
case .ended:
print("ended")
break
case .cancelled:
break
case .failed:
break
@unknown default:
break
}
}
此外,这是我的 cell configure
功能:
func configure(with viewModel: RecordsCollectionViewCellViewModel, itemCount: Int) {
longPress.numberOfTapsRequired = 1
longPress.minimumPressDuration = 0.3
longPress.delaysTouchesBegan = true
longPress.delegate = self
self.bringSubviewToFront(gestureView)
gestureView.isUserInteractionEnabled = true
gestureView.addGestureRecognizer(longPress)
}
gestureView
是单元格顶部的透明视图。
我过去尝试过这个但遇到了问题,我相信与实际 collectionView 上的 contextMenu 交互有关。
您可以通过将手势处理程序放在 collectionView 本身上并获取手势位置来修复它,然后根据此 link;
定位单元格如果您正在寻找最佳解决方案,我认为有更好的解决方案,例如将 UILongPressGestureRecognizer
添加到集合视图并确定哪个单元格被点击,或者您是否需要在 long 上显示一些选项点击 func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath
并说明推荐它们的原因,因为集合视图上已经有一些手势管理。
但是,如果您已经考虑了所有这些并且仍然觉得您的解决方案适合您,我认为问题出在这一行 let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
上,它似乎与单元格一起默认初始化.
我觉得也许当单元格被重用时,func configure
我会做这些改变:
func configure(with viewModel: RecordsCollectionViewCellViewModel, itemCount: Int) {
// Remove any previous gesture recognizers, maybe this is not needed
gestureView.gestureRecognizers?.removeAll()
// Initialize a new gesture recognizer
let longPress = UILongPressGestureRecognizer(target: self,
action: #selector(handleLongPress))
// longPress.numberOfTapsRequired = 1 - not needed
longPress.minimumPressDuration = 0.3
// longPress.delaysTouchesBegan = true - not needed
// longPress.delegate = self - not needed unless you are doing something else
gestureView.isUserInteractionEnabled = true
gestureView.addGestureRecognizer(longPress)
}
试一试,看看你的函数 handleLongPress
是否被调用了?就我而言,这是可行的。
只在viewDidLoad()中调用setLongPress()
func setLongPress(){
let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.CollectionView.addGestureRecognizer(lpgr)
}
@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer?) {
if gestureRecognizer?.state != .ended {
return
}
let p = gestureRecognizer?.location(in: projectCollectionView)
let indexPath = CollectionView.indexPathForItem(at: p ?? CGPoint.zero)
if indexPath == nil {
print("couldn't find index path")
} else {
// get the cell at indexPath (the one you long pressed)
var cell: UICollectionViewCell? = nil
if let indexPath = indexPath {
// your action here
}
// do stuff with the cell
}
}