How to call tap gestures on UIView programmatically in Swift?


In Swift, you can use the UITapGestureRecognizer class to add a tap gesture on view programmatically. This class provides you with different properties and methods to enable a tap gesture. In this article, you will learn how to add a tap gesture with an example.

UITapGestureRecognizer class

UITapGestureRecognizer is a built-in class in the UIKit framework that recognizes a tap gesture on a view. A tap gesture is a quick touch with a single finger or multiple fingers on the screen. UITapGestureRecognizer recognizes taps of a certain number of fingers, taps a certain number of times, and a combination of both.

To use the class, you have to create an instance and configure some attributes which are required to enable a tap gesture. After that, you have to add a target selector in order to handle the tap event while clicking on the object. This step is required to implement to handle the events.

Example

Here's an example of how to create and use a UITapGestureRecognizer −

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
tapGesture.numberOfTapsRequired = 2
tapGesture.numberOfTouchesRequired = 1
view.addGestureRecognizer(tapGesture)
view.isUserInteractionEnabled = true
@objc private func handleTapGesture() {
   print("tap gesture executed...")
}

Output

tap gesture executed…

In this example, a UITapGestureRecognizer is created with the target set to self, meaning the current view controller is the target object, and action set to #selector(handleTapGesture), meaning the handleTapGesture method will be called when the tap gesture is recognized. The numberOfTapsRequired property is set to 2, meaning the tap gesture will recognize a double tap, and the numberOfTouchesRequired property is set to 1, meaning the tap gesture will recognize a tap with a single finger. Finally, the tap gesture recognizer is added to the view using the addGestureRecognizer method.

Here are more details on how to customize the behavior of the tap gesture recognizer

  • Number of taps required − By default, a tap gesture recognizer recognizes a single tap. You can change this by setting the numberOfTapsRequired property to a different value.

  • Number of fingers required − By default, a tap gesture recognizer recognizes a tap with a single finger. You can change this by setting the numberOfTouchesRequired property to a different value.

  • Cancelling touches in progress − By default, a tap gesture recognizer cancels any touches in progress when it recognizes a tap. You can change this behavior by setting the cancelsTouchesInView property to false.

Conclusion

In conclusion, UITapGestureRecognizer is a built-in class in the UIKit framework that recognizes a tap gesture on a view. You can create an instance of the class, configure its properties as needed, and add it to the view to recognize taps. You can customize the behavior of the tap gesture recognizer by setting its numberOfTapsRequired, numberOfTouchesRequired, cancelsTouchesInView, and requireGestureRecognizerToFail properties.

When the user taps the view, the recognizer sends a message to its target object, which can be a view controller or another object that has been set up to handle the tap. UITapGestureRecognizer is a simple and useful way to add interactivity to your iOS app, and you can combine it with other gesture recognizers and UIKit classes to create more complex and interesting user interactions.

Updated on: 04-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements