Swift3 iOS - How to make UITapGestureRecognizer trigger function
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A UITapGestureRecognizer triggers a function through the target-action pattern. In Swift 3, the usual setup is to create the recognizer with a target and selector, add it to a view, and make sure the view can actually receive touches. If any of those parts are missing, the tap handler will never fire.
Basic Setup
A minimal Swift 3 example looks like this:
The important pieces are:
- '
target: selfso the recognizer knows which object receives the action' - '
#selector(handleTap(_:))so UIKit can call the method' - '
@objcon the method because the selector uses Objective-C runtime dispatch'
Without @objc, Swift 3 will not expose the method correctly for target-action.
Attaching the Recognizer to the Right View
The recognizer only fires when attached to a view that receives touch events. Often that is the controller's main view, but it can also be a button container, image view, label, or custom subview.
This matters because developers sometimes create the recognizer correctly but add it to the wrong view or forget to add it at all.
If you want only a specific area to react to taps, attach the recognizer to that specific subview rather than the entire screen.
Enable User Interaction When Needed
Some UIKit views, especially UIImageView and UILabel, have isUserInteractionEnabled disabled by default. If you add a gesture recognizer to one of those and do nothing else, the action will not trigger.
This is one of the most common reasons a seemingly correct gesture recognizer appears broken.
Configuring Number of Taps
You can control how many taps are required before the action fires.
This is useful when single-tap and double-tap gestures have different meanings.
Reading the Tap Location
Gesture recognizers become more useful when you inspect where the user tapped.
That makes it easy to select objects, dismiss overlays, or drive custom view logic.
Interaction With Other Controls
If the tapped view contains buttons or other recognizers, gesture conflicts can occur. In those cases, you may need recognizer delegate methods or a different view hierarchy strategy.
For example, attaching a tap recognizer to a large container view can interfere with touches intended for controls inside it. The fix is often to narrow the recognizer's scope rather than forcing everything through one gesture handler.
Common Pitfalls
The biggest mistake is forgetting @objc on the action method in Swift 3. Without it, the selector cannot be called properly.
Another common issue is adding the recognizer to a view that does not have user interaction enabled, especially UIImageView and UILabel.
Developers also sometimes mismatch the selector signature and the method signature. If the selector includes (:), the method should accept the recognizer parameter.
Finally, be careful about where the recognizer is attached. A correctly configured tap recognizer on the wrong view still will not help.
Summary
- Create the recognizer with a target and
#selector. - Mark the handler with
@objcin Swift 3. - Add the recognizer to a view that receives touch events.
- Enable
isUserInteractionEnabledfor views such as image views when needed. - Match the action method signature to the selector and use recognizer settings for tap count or location.
Related reading
- Swift - Cast Int into enumInt
- Swift - Cast Int into enumInt
- Swift - check if a timestamp is yesterday, today, tomorrow, or X days ago
- Swift - check if a timestamp is yesterday, today, tomorrow, or X days ago
- Swift - class method which must be overridden by subclass
- Swift - Convert to absolute value
- Swift - Convert to absolute value
- Swift - encode URL
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.