[英]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.