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

问题描述 投票:0回答:1

当前,我在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")
            }
        }
    }

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

ios swift uicollectionview uicollectionviewcell uigesturerecognizer
1个回答
1
投票

然后做

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

当你藏起来的时候也做

menuShown = false
© www.soinside.com 2019 - 2024. All rights reserved.