繁体   English   中英

长按后如何在 UICollectionViewCell 上禁用 UILongPressGestureRecognizer?

[英]How to disable UILongPressGestureRecognizer on UICollectionViewCell after there is a long press?

目前,我在cellForItemAt的单元格上有一个带有 UILongPressGestureRecognizer 的集合视图:

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressOnCell))
    cell.addGestureRecognizer(longPress)

当用户按住一个单元格时,它会触发一个函数来显示一个名为cellDeleteAppear()的菜单。 但是,在菜单出现在屏幕上之后,用户可以按住另一个单元格,这将导致菜单再次弹出。

@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")
            }
        }
    }

我的目标是:当菜单处于活动状态时,用户不应该按住另一个单元格来触发菜单弹出。 任何帮助表示赞赏!

然后做

var menuShown = false
@objc func handleLongPressOnCell(_ sender: UILongPressGestureRecognizer) {
   if sender.state == .began {
      guard !menuShown else { return }
      menuShown = true

当你隐藏它时

menuShown = false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM