How to call gesture tap on UIView programmatically in swift

351,259

Solution 1

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()

Solution 2

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
@objc func handleTap(_ sender: UITapGestureRecognizer) {
     print("Hello World")
  }

Solution 3

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

Solution 4

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)

Solution 5

Implementing tap gesture

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "touchHappen") 
view.userInteractionEnabled = true
view.addGestureRecognizer(tap)

Calls this function when the tap is recognized.

func touchHappen() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    self.view.endEditing(true)
}

Update for For Swift 3 +

let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchHappen(_:)))
yourView.addGestureRecognizer(tap)
yourView.userInteractionEnabled = true

func touchHappen(_ sender: UITapGestureRecognizer) {
    print("Hello Dear you are here")
}
Share:
351,259

Related videos on Youtube

George
Author by

George

I am here to gain knowledge.

Updated on July 08, 2022

Comments

  • George
    George almost 2 years

    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?

    • Wujo
      Wujo about 8 years
      In swift 2.2 you can use new selector syntax: let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
    • Fattie
      Fattie almost 8 years
      @wujo - thanks for that; strangely it doesn't actually work for me, in a UIView Maybe only in a view controller?
    • Josh
      Josh over 7 years
      Also remember to have imageView.userInteractionEnabled = true if you're using an image view. Got caught on that for too long.
    • NeverHopeless
      NeverHopeless almost 6 years
      @Josh, yes an important point.
    • Khang Azun
      Khang Azun about 4 years
      @George have you found the solution yet? I'm having the same concern with you. But my purpose is for unit test, where I want to trigger the tap on the view. Thanks
    • George
      George about 4 years
      @KhangAzun the best option is to call the tap function directly
  • George
    George about 9 years
    instead of calling selector function directly, i want to call it by programmatically invoking UITapGestureRecognizer event
  • Eric Aya
    Eric Aya about 8 years
    @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.
  • Dayanithi Natarajan
    Dayanithi Natarajan almost 8 years
    add addTapGesture() in your viewDidLoad()
  • Itai Spector
    Itai Spector about 7 years
    The note is important!
  • Daniel
    Daniel almost 7 years
    tap.delegate = self is not needed, since you already set a target and action
  • Travis M.
    Travis M. almost 6 years
    Throws an error in Swift3, your handle tap function should be: func handleTap(_ sender: UITapGestureRecognizer)
  • jose920405
    jose920405 almost 6 years
    isUserInteractionEnabled is really necessary? the default is false?
  • Prasad Patil
    Prasad Patil almost 6 years
    Calling function should be................func handleTap(_ sender: UITapGestureRecognizer? = nil) ...............you missed "_"........so your calling function was not working..
  • Illya Krit
    Illya Krit almost 6 years
    The function onSelect don't need to be IBAction
  • Illya Krit
    Illya Krit almost 6 years
    iView?.addGestureRecognizer(tap) The name of your gestureRecognizer is clickUITapGestureRecognizer So the right way is: iView?.addGestureRecognizer(clickUITapGestureRecognizer)
  • lfurini
    lfurini over 5 years
    While a code-only answer is indeed an answer, a bit of explanation (why the OP's code did not work, what is the general idea behind your code, important points, warnings, ...) could make it a better answer.
  • Pankaj Kulkarni
    Pankaj Kulkarni over 5 years
    view.isUserInteractionEnabled = true is the key when your view is ImageView or any subclass of UIVIew instead of UIControl.
  • Pankaj Kulkarni
    Pankaj Kulkarni over 5 years
    view.isUserInteractionEnabled = true is the key when your view is ImageView or any subclass of UIVIew instead of UIControl.
  • guido
    guido about 5 years
    Step 2 is not required.
  • Xav Mac
    Xav Mac about 3 years
    You can delete the lines "isUserInteractionEnabled = true" and put the value directly on Attributes Inspector, just below "Tag" you have "User Interaction Enabled" with a check-box.