How to call gesture tap on UIView programmatically in swift
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")
}
Related videos on Youtube
Comments
-
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 about 8 yearsIn swift 2.2 you can use new selector syntax: let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
-
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 over 7 yearsAlso remember to have
imageView.userInteractionEnabled = true
if you're using an image view. Got caught on that for too long. -
NeverHopeless almost 6 years@Josh, yes an important point.
-
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 about 4 years@KhangAzun the best option is to call the tap function directly
-
-
George about 9 yearsinstead of calling selector function directly, i want to call it by programmatically invoking UITapGestureRecognizer event
-
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 almost 8 yearsadd
addTapGesture()
in yourviewDidLoad()
-
Itai Spector about 7 yearsThe note is important!
-
Daniel almost 7 years
tap.delegate = self
is not needed, since you already set a target and action -
Travis M. almost 6 yearsThrows an error in Swift3, your handle tap function should be: func handleTap(_ sender: UITapGestureRecognizer)
-
jose920405 almost 6 years
isUserInteractionEnabled
is really necessary? the default is false? -
Prasad Patil almost 6 yearsCalling function should be................func handleTap(_ sender: UITapGestureRecognizer? = nil) ...............you missed "_"........so your calling function was not working..
-
Illya Krit almost 6 yearsThe function onSelect don't need to be IBAction
-
Illya Krit almost 6 yearsiView?.addGestureRecognizer(tap) The name of your gestureRecognizer is clickUITapGestureRecognizer So the right way is: iView?.addGestureRecognizer(clickUITapGestureRecognizer)
-
lfurini over 5 yearsWhile 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 over 5 years
view.isUserInteractionEnabled = true
is the key when your view is ImageView or any subclass of UIVIew instead of UIControl. -
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 about 5 yearsStep 2 is not required.
-
Xav Mac about 3 yearsYou 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.