gmdev
gmdev

Reputation: 3155

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:

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(). 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!

Upvotes: 1

Views: 180

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

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

Upvotes: 2

Related Questions