How to add a double-touch gesture recognizer in Swift

I have already done a single recognition, but I can’t figure out how to make this peer recognizer double-click. I could use some recommendations.

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) } } 
+6
source share
5 answers

I solved this with the 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") } } 
+1
source

Try the code below

 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) } } 
+8
source
  // 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 three lines

  singleTap.require(toFail: doubleTap) singleTap.delaysTouchesBegan = true doubleTap.delaysTouchesBegan = true 
+7
source

Here is a sample code for single and double action.

Note that a single click has a colon ( handleSingleTap: , but the double-click action is not ( handleDoubleTap )? This means that the selector takes an argument. Thus, in the functions below, we can check the sender of one tap, which would be useful if you assigned the tap function to many elements. If you have a colon, you must have 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: in this example, a single click action will be performed twice.

+5
source

Faster way:

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

Closure:

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

It is very important not to lose the link to your action (you can do this as a field, as in the above example).

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

0
source

All Articles