Swift
UIView
Gesture Recognition
Tap Gesture
iOS Development

How to call gesture tap on UIView programmatically in swift

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want the same behavior as a tap gesture programmatically, the usual answer is not to "fire the gesture recognizer" itself. The cleaner approach is to move the tap logic into a normal method and call that method both from the gesture recognizer and from your code.

A gesture recognizer exists to translate user input into callbacks. It is not usually the object you should simulate directly.

Set Up the Tap Gesture Normally

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: 40, y: 100, width: 120, height: 120)
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        runTapAction()
20    }
21
22    private func runTapAction() {
23        print("tap behavior executed")
24    }
25}

The important part is that the real work lives in runTapAction() rather than inside gesture-specific plumbing.

Call the Shared Action Programmatically

Now you can invoke the same behavior from anywhere:

swift
runTapAction()

This gives you the same effect as a user tap without trying to fake a touch event.

That is usually the best answer to the question.

Why Not Trigger the Recognizer Directly

A gesture recognizer manages state transitions based on real touch input. It is not designed as a general-purpose event bus.

You may find hacky ways to call the selector manually, but that is not the same thing as the gesture recognizer successfully recognizing a tap in the UIKit input system.

So if your goal is business behavior, call the business behavior method. If your goal is UI testing, use a UI testing framework to perform a real tap.

Calling the Selector Manually

If you truly want to invoke the target method yourself, you can call it directly.

swift
let fakeTap = UITapGestureRecognizer()
handleTap(fakeTap)

This is valid Swift, but it is only manually calling the handler method. It is not simulating a real user gesture lifecycle, location, or recognizer state progression.

For most codebases, this is less clean than calling a shared helper method such as runTapAction().

Programmatic Interaction in Tests

If the real goal is testing that a tap works, prefer UI tests or unit tests on the extracted action logic.

For unit testing:

  • test runTapAction() directly
  • keep gesture wiring thin

For UI testing:

  • use XCTest UI interaction APIs to perform an actual tap on the view or control

That separation makes both production code and tests easier to maintain.

Common Pitfalls

The biggest mistake is putting all tap behavior directly inside the gesture callback and then later wanting to trigger it from somewhere else.

Another common issue is trying to "simulate" UIKit gesture recognition by manually creating recognizers. That only calls methods; it does not reproduce the full touch system.

People also forget that ordinary UIView instances need isUserInteractionEnabled = true if they are expected to receive gesture input.

Finally, if the view is actually a button-like control, sometimes a control event is a better abstraction than a custom tap recognizer.

Summary

  • The clean solution is to extract the tap behavior into a normal method.
  • Let the gesture recognizer call that method for real taps.
  • Call the same method directly when you want the behavior programmatically.
  • Manually calling the gesture handler is possible but not the same as simulating UIKit input.
  • Keep gesture callbacks thin and business logic separate.
  • Use UI tests when you need to verify real tap interaction.

Course illustration
Course illustration

All Rights Reserved.