[英]How to disable UILongPressGestureRecognizer on UICollectionViewCell after there is a long press?
Currently, I have a collection view with a UILongPressGestureRecognizer on the cell in cellForItemAt
:目前,我在
cellForItemAt
的单元格上有一个带有 UILongPressGestureRecognizer 的集合视图:
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressOnCell))
cell.addGestureRecognizer(longPress)
When the user holds down on a cell, it triggers a function to show a menu called cellDeleteAppear()
.当用户按住一个单元格时,它会触发一个函数来显示一个名为
cellDeleteAppear()
的菜单。 However, after the menu is on the screen, the user can then hold down on another cell which will cause the menu to pop up again.但是,在菜单出现在屏幕上之后,用户可以按住另一个单元格,这将导致菜单再次弹出。
@objc func handleLongPressOnCell(_ sender: UILongPressGestureRecognizer) {
if sender.state == .began {
cellDeleteAppear()
let gestureLocation = sender.location(in: self.trayCollectionView)
if let indexPath = self.trayCollectionView.indexPathForItem(at: gestureLocation) {
indexPathForDeletion = indexPath
trayCollectionView.allowsSelection = false
} else {
print("long press error at index path")
}
}
}
My goal is: while the menu is active, the user should not be able to hold down on another cell to trigger the menu to pop up.我的目标是:当菜单处于活动状态时,用户不应该按住另一个单元格来触发菜单弹出。 Any help is appreciated!
任何帮助表示赞赏!
Then do然后做
var menuShown = false
@objc func handleLongPressOnCell(_ sender: UILongPressGestureRecognizer) {
if sender.state == .began {
guard !menuShown else { return }
menuShown = true
And when you hide it do当你隐藏它时
menuShown = false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.