Swift3
iOS Development
UITapGestureRecognizer
Gesture Recognizer
Mobile Programming

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.

Browse interview questions

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:

swift
1import UIKit
2
3class ViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
8        view.addGestureRecognizer(tap)
9    }
10
11    @objc func handleTap(_ sender: UITapGestureRecognizer) {
12        print("View was tapped")
13    }
14}

The important pieces are:

  • 'target: self so the recognizer knows which object receives the action'
  • '#selector(handleTap(_:)) so UIKit can call the method'
  • '@objc on 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.

swift
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
myCustomView.addGestureRecognizer(tap)

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.

swift
1imageView.isUserInteractionEnabled = true
2let tap = UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:)))
3imageView.addGestureRecognizer(tap)
4
5@objc func imageTapped(_ sender: UITapGestureRecognizer) {
6    print("Image tapped")
7}

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.

swift
1let tap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(_:)))
2tap.numberOfTapsRequired = 2
3view.addGestureRecognizer(tap)
4
5@objc func handleDoubleTap(_ sender: UITapGestureRecognizer) {
6    print("Double tap detected")
7}

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.

swift
1@objc func handleTap(_ sender: UITapGestureRecognizer) {
2    let point = sender.location(in: view)
3    print("Tapped at \(point)")
4}

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 @objc in Swift 3.
  • Add the recognizer to a view that receives touch events.
  • Enable isUserInteractionEnabled for 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.