UITapGestureRecognizer - Single Tap & Double Tap Example in Swift 4 - Swift 4 Tutorials W3Schools

Hot

Post Top Ad

11 Nov 2017

UITapGestureRecognizer - Single Tap & Double Tap Example in Swift 4

The iOS UITapGestureRecognizer class has a built-in way to detect taps on any view. We can set the required taps based on our usage.

UITapGestureRecognizer - Single Tap & Double Tap Example in Swift 4

Other Gestures in iOS:

Swipe Gesture Recognizer - UISwipeGestureRecognizer
Pinch Gesture Recognizer - UIPinchGestureRecognizer

Pan Gesture Recognizer - UIPanGestureRecognizer
Long Press Gesture - UILongPressGestureRecognizer
UIRotationGestureRecognizer - Rotate Image View swift

Single Tap Gesture:

For detecting single tap gesture, here's an example:

override func viewDidLoad() {
    super.viewDidLoad()
    
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(singleTap(gesture:)))
    view.addGestureRecognizer(tapGesture)
}

@objc func singleTap(gesture: UITapGestureRecognizer) {
    print("single tap called")
}


Double Tap Gesture:

For detecting Double tap gesture is similar to above, just we need to set numberOfTapsRequired to 2.

Here's an example:

override func viewDidLoad() {
    super.viewDidLoad()
    
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(doubleTap(gesture:)))
    tapGesture.numberOfTapsRequired = 2
    view.addGestureRecognizer(tapGesture)
}

@objc func doubleTap(gesture: UITapGestureRecognizer) {
    print("double tap called")
}

No comments:

Post a Comment

Post Top Ad