[英]Adding UILongPressGesture to Cell not CollectionView
我正在尝试将UILongGestureRecognizer添加到我的自定义 CollectionView 单元中,但处理程序 function从未调用过。 这是来自自定义单元格的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
function:
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 本身上并获取手势位置来修复它,然后按照此链接将单元格放在位置;
如果您正在寻找最佳解决方案,我认为有更好的解决方案,例如将UILongPressGestureRecognizer
添加到集合视图并确定点击了哪个单元格,或者您是否需要在长按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)
}
试试看,现在是否正在调用 function 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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.