Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a double tap Gesture Recognizer in Swift

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)
    }
}
like image 973
Hunter Avatar asked Aug 28 '15 03:08

Hunter


People also ask

How do I add tap gestures in swift 5?

Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

What is onTapGesture SwiftUI?

onTapGesture { print("VStack tapped") } } } In this situation SwiftUI will always give the child's gesture priority, which means when you tap the text view above you'll see “Text tapped”.

What is UITapGestureRecognizer Swift?

UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer . For gesture recognition, the specified number of fingers must tap the view a specified number of times. Although taps are discrete gestures, they're discrete for each state of the gesture recognizer.


3 Answers

     // 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
like image 52
Spydy Avatar answered Oct 22 '22 08:10

Spydy


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")
    }
}
like image 32
Marian König Avatar answered Oct 22 '22 07:10

Marian König


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)
}
}
like image 9
Ramkumar chintala Avatar answered Oct 22 '22 07:10

Ramkumar chintala