0

I'm trying to add UILongPressGesture to the cell. It is working but only when I long press and move in any direction. It should call the selector method on long press but it is calling when I long press and started to move. I'm also handling the state of the gesture but selector is not calling until I long press and start moving.

I have also tried with adding gesture to cell's content view and its UIlable element, but no luck.

let longPressGesture: UILongPressGestureRecognizer = {
                    let gesture = UILongPressGestureRecognizer()
                    gesture.addTarget(self, action: #selector(MyViewController.handleLongPressGetureForRow(_:)))
                    gesture.delaysTouchesBegan = false
                    gesture.cancelsTouchesInView = false
                    gesture.numberOfTouchesRequired = 1
                    gesture.minimumPressDuration = 0.2
                    return gesture
                }()
                cell.addGestureRecognizer(longPressGesture)
                cell.tag = indexPath.row

Cells where adding gesture

Please help me. Thanks in advance.

V. Garg
  • 15
  • 6

1 Answers1

0

Instead of handleLongPressGetureForRow(_:) change to self.handleLongPressGetureForRow(v:) with @objc before func

   let longPressGesture: UILongPressGestureRecognizer = {
        let gesture = UILongPressGestureRecognizer()
        gesture.addTarget(self, action: #selector(self.handleLongPressGetureForRow(v:)))
        gesture.delaysTouchesBegan = false
        gesture.cancelsTouchesInView = false
        gesture.numberOfTouchesRequired = 1
        gesture.minimumPressDuration = 0.2
        return gesture
    }()
    cell.addGestureRecognizer(longPressGesture)
    cell.tag = indexPath.row

    return cell
}
@objc func handleLongPressGetureForRow(v: UILongPressGestureRecognizer )
{
   print("saghsaghghsgfsgsaghghsaghsaghghsaghashgsasa")
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • I've added **@objc** already, without **@objc** it gives compile time error. And using `self.handleLongPressGestureForRow(_:)` instead of `MyViewcontroller.handleLongPressGestureForRow(_:)` doesn't solve the problem. I've mentioned that it is working but only when I started moving after long press. – V. Garg Feb 12 '18 at 09:40