Swift: Long Press Gesture Recognizer - Detect taps and Long Press Swift: Long Press Gesture Recognizer - Detect taps and Long Press ios ios

Swift: Long Press Gesture Recognizer - Detect taps and Long Press


Define two IBActions and set one Gesture Recognizer to each of them. This way you can perform two different actions for each gesture.

You can set each Gesture Recognizer to different IBActions in the interface builder.

@IBAction func tapped(sender: UITapGestureRecognizer){    println("tapped")    //Your animation code.}@IBAction func longPressed(sender: UILongPressGestureRecognizer){    println("longpressed")    //Different code}

Through code without interface builder

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")    self.view.addGestureRecognizer(tapGestureRecognizer)    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")    self.view.addGestureRecognizer(longPressRecognizer)func tapped(sender: UITapGestureRecognizer){    println("tapped")}func longPressed(sender: UILongPressGestureRecognizer){    println("longpressed")}

Swift 5

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))self.view.addGestureRecognizer(tapGestureRecognizer)    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))self.view.addGestureRecognizer(longPressRecognizer)    @objc func tapped(sender: UITapGestureRecognizer){    print("tapped")}@objc func longPressed(sender: UILongPressGestureRecognizer) {    print("longpressed")}


For swift2

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))lpgr.minimumPressDuration = 0.5lpgr.delaysTouchesBegan = truelpgr.delegate = selfself.featuredCouponColView.addGestureRecognizer(lpgr)

Action

//MARK: - UILongPressGestureRecognizer Action -    func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {        if gestureReconizer.state != UIGestureRecognizerState.Ended {            //When lognpress is start or running        }        else {            //When lognpress is finish        }    }

For Swift 4.2/ Swift 5

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))lpgr.minimumPressDuration = 0.5lpgr.delaysTouchesBegan = truelpgr.delegate = selfself.colVw.addGestureRecognizer(lpgr)//MARK: - UILongPressGestureRecognizer Action -    @objc func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {        if gestureReconizer.state != UIGestureRecognizer.State.ended {            //When lognpress is start or running        }        else {            //When lognpress is finish        }    }


Through code without interface builder

// Global variables declarationvar longPressed = falsevar selectedRow = 0override func viewDidLoad() {        super.viewDidLoad()          let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ContactListTableViewController.handleLongPress(_:)))        longPressGesture.minimumPressDuration = 1.0 // 1 second press        longPressGesture.allowableMovement = 15 // 15 points        longPressGesture.delegate = self        self.tableView.addGestureRecognizer(longPressGesture)    }// Long tap work goes here !!if (longPressed == true) {       if(tableView.cellForRowAtIndexPath(indexPath)?.accessoryType == .Checkmark){                tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .None                self.selectedRow -= 1                if(self.selectedRow == 0){                    self.longPressed = false                }            } else {                self.selectedRow += 1                tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark            }        } else if(self.selectedRow == 0) {          // Single tape work goes here !!        }

But the only problem is the long press gesture runs two times. If you have found any solution do comment below !