How to call gesture tap on UIView programmatically in swift

swift uiview on tap
tap gesture in swift 3
swift tap gesture sender
add double tap gesture swift
swift tap gesture recognizer image
swift tap gesture recognizer location
add tap gesture recognizer to uitextfield swift
add tap gesture recognizer programmatically objective-c

I have a UIView and and I have added tap gesture to it:

let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
myView.addGesture(tap)

I am trying to call it programmatically in the testfile.

sendActionForEvent

I am using this function, but it is not working:

myView.sendActionForEvent(UIEvents.touchUpDown)

It shows unrecognised selector sent to instance.

How can I solve this problem?

You need to initialize UITapGestureRecognizer with a target and action, like so:

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

Then, you should implement the handler, which will be called each time when a tap event occurs:

@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
    // handling code
}

So now calling your tap gesture recognizer event handler is as easy as calling a method:

handleTap()

How to call gesture tap on UIView programmatically in swift, I have a UIView and and I have added tap gesture to it: let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:")) tap.​delegate = self myView. How to call gesture tap on UIView programmatically in swift (16) I have a UIView and and I have added tap gesture to it: let tap = UITapGestureRecognizer (target: self, action: Selector ("handleTap:")) tap. delegate = self myView. addGesture (tap)

For anyone who is looking for Swift 3 solution

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

view.addGestureRecognizer(tap)

view.isUserInteractionEnabled = true

self.view.addSubview(view)

// function which is triggered when handleTap is called
 func handleTap(_ sender: UITapGestureRecognizer) {
     print("Hello World")
  }

How to call gesture tap on UIView programmatically in swift , I have a UIView and and I have added tap gesture to it: let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:")) tap.​delegate = self myView. For your tap gesture to work, you have to set the number of taps. Add this line: tapGesture.numberOfTapsRequired = 1 (I'm assuming that tapGesture is the same one you call label.addGestureRecognizer(tapGesture) with)

For Swift 4:

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

view.addGestureRecognizer(tap)

view.isUserInteractionEnabled = true

self.view.addSubview(view)

// function which is triggered when handleTap is called
@objc func handleTap(_ sender: UITapGestureRecognizer) {
    print("Hello World")
}

In Swift 4, you need to explicitly indicate that the triggered function is callable from Objective-C, so you need to add @objc too your handleTap function.

See @Ali Beadle 's answer here: Swift 4 add gesture: override vs @objc

How to call gesture tap on UIView programmatically in swift 5.1 , I have a UIView and and I have added tap gesture to it: let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))  Quelle est la différence entre les attributs atomiques et non atomiques? Comment faire bouger un UITextField lorsque le clavier est présent? Utilisation de la mise en page automatique dans UITableView pour la mise en page dynamique des cellules et les hauteurs variables des rangées

Just a note - Don't forget to enabled interaction on the view:

let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))

view.addGestureRecognizer(tap)

// view.userInteractionEnabled = true

self.view.addSubview(view)

Handling Tap Gestures, Swift; Objective-C Tap gestures detect one or more fingers touching the screen briefly. Programmatically. Call the addGestureRecognizer(_:) method of your view. if gestureRecognizer.state == .ended { // Move the view down and to the right when tapped. let animator = UIViewPropertyAnimator(duration: 0.2, curve: . Adding a Gesture Programmatically. To add a gesture programmatically, you (1) create a gesture recognizer, (2) add it to a view, and (3) make a method that is called when the gesture is recognized. import UIKit class ViewController: UIViewController { @IBOutlet weak var myView: UIView! override func viewDidLoad()

STEP : 1

@IBOutlet var viewTap: UIView!

STEP : 2

var tapGesture = UITapGestureRecognizer()

STEP : 3

override func viewDidLoad() {
    super.viewDidLoad()
    // TAP Gesture
    tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myviewTapped(_:)))
    tapGesture.numberOfTapsRequired = 1
    tapGesture.numberOfTouchesRequired = 1
    viewTap.addGestureRecognizer(tapGesture)
    viewTap.isUserInteractionEnabled = true
}

STEP : 4

func myviewTapped(_ sender: UITapGestureRecognizer) {

    if self.viewTap.backgroundColor == UIColor.yellow {
        self.viewTap.backgroundColor = UIColor.green
    }else{
        self.viewTap.backgroundColor = UIColor.yellow
    }
}

OUTPUT

UIGestureRecognizer Tutorial: Getting Started, In iOS, gestures like taps, pinches, pans or rotations are used for user input. In your to your app, both within the storyboard editor in Xcode and programmatically. touchesMoved and touchesEnded — on every touch in a UIView . Open ViewController.swift and add the following method right below  The only way you can make a tap gesture recognizer work with multiple views would be to attach it to the superview, and then write extra code that figures out which of your subviews (if any) it hit. This is usually more trouble than it's worth, though, and you're better off simply creating a separate gesture recognizer for each view.

UITapGestureRecognizer Tutorial in Swift 3.0, I have a UIView and and I have added tap gesture to it: let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))  In this video, I’m going to show how I usually create UIViews in code without storyboards. I will show the simple way to add Auto Layout with anchors and an easy way to substitute IBActions. At

How to call gesture tap on UIView programmatically in swift, Swift, Swift 3.0, iOS, iOSProgramming, UITapGestureRecognizer 1 @IBOutlet var viewTap: UIView! Step 4 : Initialise the UITapGestureRecognizer and adding tap gesture in view. How to call webservice using NSURLSession? storyboard. the previous example study to Create UITableView Programatically swift 4 . In this tutorial, you will learn how to create a UICollectionView programmatically in Swift. You will learn how to create a new UICollectionView with UICollectionViewFlowLayout and will learn how to make it display a few cells. You will also learn how to make your UIViewController conform to a UICollectionViewDelagate to handle events when user taps on…

I have a UIView and and I have added tap gesture to it: let tap = UITapGestureRecognizer(target: self, action: Selector( For example, you might configure tap gesture recognizers to detect single taps, double taps, or triple taps. You can attach a gesture recognizer in one of these ways: Programmatically. Call the add Gesture Recognizer(_:) method of your view. In Interface Builder. Drag the appropriate object from the library and drop it onto your view.

Comments
  • In swift 2.2 you can use new selector syntax: let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
  • @wujo - thanks for that; strangely it doesn't actually work for me, in a UIView Maybe only in a view controller?
  • Also remember to have imageView.userInteractionEnabled = true if you're using an image view. Got caught on that for too long.
  • @Josh, yes an important point.
  • instead of calling selector function directly, i want to call it by programmatically invoking UITapGestureRecognizer event
  • @muhasturk Don't change the meaning of an upvoted answer. Your edit was invalidating the answer for older Swift versions. If you want to post an update for Swift 2.2, post your own answer. Thank you.
  • tap.delegate = self is not needed, since you already set a target and action
  • Throws an error in Swift3, your handle tap function should be: func handleTap(_ sender: UITapGestureRecognizer)
  • Calling function should be................func handleTap(_ sender: UITapGestureRecognizer? = nil) ...............you missed "_"........so your calling function was not working..
  • isUserInteractionEnabled is really necessary? the default is false?