17

I have already accomplished a single tap recognizer but can not figure out how to make that single tap recognizer a double tap instead. I could use some guidance.

Code:

import Foundation
import UIKit

class MainBoardController: UIViewController{

    let tap = UITapGestureRecognizer()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "GotoProfile")
        swipe.direction = UISwipeGestureRecognizerDirection.Right
                    self.view.addGestureRecognizer(swipe)

        tap.addTarget(self, action: "GotoCamera")
        view.userInteractionEnabled = true
        view.addGestureRecognizer(tap)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func GotoProfile(){
        self.performSegueWithIdentifier("Profilesegue", sender: nil)
    }

    func GotoCamera(){
        self.performSegueWithIdentifier("Camerasegue", sender: nil)
    }
}
2
  • tap.numberOfTapsRequired = 2
    – vacawama
    Commented Aug 28, 2015 at 3:22
  • 1
    Use UITapGestureRecognizer for taps, not UISwipeGestureRecognizer. Unless you mean two-finger swipe and not double-tap.
    – rmaddy
    Commented Aug 28, 2015 at 3:42

5 Answers 5

30
     // Single Tap
    let singleTap: UITapGestureRecognizer =  UITapGestureRecognizer(target: self, action: #selector(PostDetailViewController.handleSingleTap(sender:)))
    singleTap.numberOfTapsRequired = 1
    self.viewTop.addGestureRecognizer(singleTap)

    // Double Tap
    let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PostDetailViewController.handleDoubleTap))
    doubleTap.numberOfTapsRequired = 2
    self.viewTop.addGestureRecognizer(doubleTap)

    singleTap.require(toFail: doubleTap)
    singleTap.delaysTouchesBegan = true
    doubleTap.delaysTouchesBegan = true

   func handleSingleTap(sender: AnyObject?) {

    print("Single Tap!")

   }
   func handleDoubleTap() {

    print("Double Tap!")
   }

Magic in below three lines

    singleTap.require(toFail: doubleTap)
    singleTap.delaysTouchesBegan = true
    doubleTap.delaysTouchesBegan = true
1
  • Very nice: do you know, how to implement this in SwiftUI ? Commented Dec 13, 2020 at 20:26
14

I solved this with an extension:

override func viewDidLoad() {
    super.viewDidLoad()
            
    let tapGR = UITapGestureRecognizer(target: self, action: #selector(PostlistViewController.handleTap(_:)))
    tapGR.delegate = self
    tapGR.numberOfTapsRequired = 2
    view.addGestureRecognizer(tapGR)
}

extension MainBoardController: UIGestureRecognizerDelegate {
    func handleTap(_ gesture: UITapGestureRecognizer){
        print("doubletapped")
    }
}
9

Try Below Code

import UIKit

class MainBoardController: UIViewController{

let tap = UITapGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "GotoProfile")
    swipe.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipe)

    // DOUBLE TAP
    tap.numberOfTapsRequired = 2
    tap.addTarget(self, action: "GotoCamera")
    view.userInteractionEnabled = true
    view.addGestureRecognizer(tap)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func GotoProfile(){
    self.performSegueWithIdentifier("Profilesegue", sender: nil)
}

func GotoCamera(){
    self.performSegueWithIdentifier("Camerasegue", sender: nil)
}
}
6

Here is some example code for a single and double tap action.

Notice that the single tap action has a colon (handleSingleTap:) but the double tap action does not (handleDoubleTap)? This indicates that the selector takes an argument. So in the functions below, we can check the sender of the single tap which would be useful if you were assigning the tap function to many elements. If you have the colon you must have the parameters in the function.

override func viewDidLoad() {
    super.viewDidLoad()

    // Single Tap
    let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleSingleTap:")
    singleTap.numberOfTapsRequired = 1
    self.view.addGestureRecognizer(singleTap)

    // Double Tap
    let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleDoubleTap")
    doubleTap.numberOfTapsRequired = 2
    self.view.addGestureRecognizer(doubleTap)
}

func handleSingleTap(sender: AnyObject?) {
    print("Single Tap!")
}
func handleDoubleTap() {
    print("Double Tap!")
}

Note: this example will run the single tap action in a double tap.

0

More Swifty way:

view?.tap(numberOfTapsRequired: 2, action: action)

Closure:

private lazy var action: Action = Action(action: {[weak self] _ in
    print("Double click")
})

It's very important not to lose reference to your action (you can make it as field like in above example).

by using this UIView extension: https://gist.github.com/yoman07/7aae5abb957ff656b2f118ad1e6c2d36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.