Swift
UIView
gesture tap
programming
iOS development

How to call gesture tap on UIView programmatically in swift

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

There are two different tasks people mean when they say they want to "call a tap gesture programmatically" on a UIView. One is attaching a UITapGestureRecognizer in code so real user taps are recognized, and the other is invoking the same tap-handling logic from code without an actual touch event.

Add A Tap Recognizer In Code

The normal way to respond to user taps is to add a UITapGestureRecognizer to the view.

swift
1import UIKit
2
3final class DemoViewController: UIViewController {
4    let box = UIView()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8
9        box.frame = CGRect(x: 80, y: 120, width: 160, height: 160)
10        box.backgroundColor = .systemBlue
11        box.isUserInteractionEnabled = true
12        view.addSubview(box)
13
14        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
15        box.addGestureRecognizer(tap)
16    }
17
18    @objc private func handleTap(_ recognizer: UITapGestureRecognizer) {
19        recognizer.view?.backgroundColor = .systemGreen
20    }
21}

This is the standard solution when you want actual taps from the user.

If You Want To Trigger The Same Behavior In Code

A gesture recognizer is meant to represent user interaction, not to be manually fired like a button event. The clean way to "call it programmatically" is to move the useful behavior into a normal method and call that method both from the gesture handler and from your code.

swift
1import UIKit
2
3final class DemoViewController: UIViewController {
4    let box = UIView()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8
9        box.frame = CGRect(x: 80, y: 120, width: 160, height: 160)
10        box.backgroundColor = .systemBlue
11        box.isUserInteractionEnabled = true
12        view.addSubview(box)
13
14        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
15        box.addGestureRecognizer(tap)
16
17        performTapAction()
18    }
19
20    @objc private func handleTap(_ recognizer: UITapGestureRecognizer) {
21        performTapAction()
22    }
23
24    private func performTapAction() {
25        box.backgroundColor = .systemGreen
26    }
27}

This keeps the business logic separate from the gesture object itself.

Why Not Simulate The Gesture Directly

You generally should not try to force a UITapGestureRecognizer through its internal state transitions just to simulate a user tap. UIKit gesture recognizers are part of the touch event system, and artificially toggling them is brittle and not the intended API.

If you need testing, use UI testing tools. If you need shared behavior, call a normal method.

Multiple Tap Configuration

If your design needs double taps or multiple fingers, configure the recognizer explicitly.

swift
1let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
2tap.numberOfTapsRequired = 2
3tap.numberOfTouchesRequired = 1
4box.addGestureRecognizer(tap)

That changes when the action fires for real user interaction.

Programmatic Invocation For Testing Or Flow Control

Sometimes a controller wants to reuse the same logic after a network response or during onboarding. In those cases, invoking performTapAction() directly is clearer than trying to imitate the recognizer event system.

That design also makes unit testing easier because the core behavior no longer depends on a synthetic gesture object.

Common Pitfalls

The most common mistake is forgetting to enable interaction on the target view. Some views accept interaction by default, but when you create custom views or overlays, interaction state can still block the gesture.

Another mistake is putting all the real behavior inside the selector method and then trying to manually trigger the recognizer itself. Put the behavior in a separate method instead.

A third issue is attaching the recognizer correctly but expecting it to work when another view or gesture recognizer is intercepting touches first.

Summary

  • Add a UITapGestureRecognizer in code when you want real user taps on a UIView.
  • If you want the same effect from code, call a normal helper method instead of trying to fake the gesture.
  • Keep gesture wiring separate from the actual UI behavior.
  • Configure tap count and touch count explicitly when needed.
  • For testing, prefer UI test tools or direct method calls over synthetic gesture hacks.

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.