I am quite new to programming and Objective C. I was wondering how to make an app which has a blank screen and a timer that goes for one minute. You are meant to tap as fast as you can and as long as you can for. I was wondering how to implement the UITapGestureRecognizer
into my code.

- 33,216
- 24
- 116
- 190

- 925
- 2
- 8
- 9
-
Help yourself: https://github.com/nhahtdh/PS3/blob/master/Game/GameBlock.m – nhahtdh Jul 06 '12 at 04:10
5 Answers
This is a step by step guide on how to implement gesture recognizers in your class:
Conform your class to
UIGestureRecognizerDelegate
protocol.Instantiate the gesture recognizer. For example, to instantiate a
UITapGestureRecognizer
, we will do:UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
Here, action is the selector which will handle the gesture. Here, our selector handleTapFrom will look something like:
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer { //Code to handle the gesture }
The argument to the selector is the gesture recognizer. We can use this gesture recognizer to access its properties, for example, we can find the state of the gesture recognizer, like,
UIGestureRecognizerStateBegan
,UIGestureRecognizerStateEnded
, etc.Set the desired properties on the instantiated gesture recognizer. For example, for a
UITapGestureRecognizer
, we can set the propertiesnumberOfTapsRequired
, andnumberOfTouchesRequired
.Add the gesture recognizer to the view you want to detect gestures for. In our sample code (I will be sharing that code for your reference), we will add gesture recognizers to an imageView with the following line of code:
[self.imageView addGestureRecognizer:tapGestureRecognizer];
After adding the gesture recognizer to the view, set the delegate for the gesture recognizer, i.e. the class which will handle all the gesture recognizer stuff. In our sample code, it would be like:
tapGestureRecognizer.delegate = self;
Note: Assign the delegate after adding the gesture recognizer to the view. Otherwise, the action method won’t be called.

- 55,989
- 15
- 126
- 162

- 4,527
- 2
- 17
- 26
-
2Nothing to be sorry about. There is a learning curve when it comes to mark-down formatting. – nhahtdh Jul 06 '12 at 04:18
-
6Since you allocate the gesture with (target:self) isn't setting it's delegate to self and conforming the UIGestureRecognizerDelegate protocol a bit redundant? I have never set the delegate nor implemented the protocol, I just implement the method with the @selector's signature and everything always worked fine for me.. – David Ben Ari Mar 07 '13 at 11:45
-
6Step 1 isn't needed. `UIGestureRecognizerDelegate` is for classes implementing a gesture recognizer, not for classes using one. – Graham Perks Jul 17 '14 at 16:55
-
@MathewVarghese - I am experiencing a weird behaviour - when i set the delegate after adding the gesture recognizer to the view - I get a bad access crash. If I add the delegate before - all works as expected. Any clue why this happens ? – Petar Nov 03 '14 at 17:44
Example in Swift:
import UIKit
class ViewController: UIViewController {
@IBOutlet var myUIImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "myUIImageViewTapped:")
singleTap.numberOfTapsRequired = 1
singleTap.numberOfTouchesRequired = 1
self.myUIImageView.addGestureRecognizer(singleTap)
self.myUIImageView.userInteractionEnabled = true
}
func myUIImageViewTapped(recognizer: UITapGestureRecognizer) {
if(recognizer.state == UIGestureRecognizerState.Ended){
println("myUIImageView has been tapped by the user.")
}
}
}

- 15,628
- 6
- 82
- 76
-
2That's right, userInteractionEnabled is essential. Thanks! – Hola Soy Edu Feliz Navidad Apr 02 '15 at 11:55
-
I don't understand why is it required to check for "recognizer.state == UIGestureRecognizerState.Ended". I tested this and it works without it as well. My assumption is that you need it for multi-tap gesture recognizer but for a single tap, do we still need it? – plam4u Jul 05 '16 at 10:16
-
The default value for numberOfTapsRequired and numberOfTouchesRequired are both 1. Those two lines in your code are unnecessary. – rosem Mar 31 '20 at 14:54
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];
self.tableView.userInteractionEnabled = YES;
gestureRecognizer.cancelsTouchesInView = NO; // this prevents the gesture recognizers to 'block' touches

- 17,282
- 18
- 107
- 195
Works in Xcode 11.4, Swift 5
class ViewController: UIViewController {
@IBOutlet weak var targetView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchCallback(_:)))
self.targetView.addGestureRecognizer(tap)
}
@objc func touchCallback(_ sender: UITapGestureRecognizer? = nil) {
if(sender?.state == .ended) {
print("tapped")
}
}
}
// If you want to touch start, available touchesBegan
class ViewController: UIViewController {
@IBOutlet weak var targetView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: targetView)
print(location)
}
}
}

- 1,515
- 1
- 16
- 19
If you are working Swift (iOS9) and SpriteKit, try the following.
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let singleTap = UITapGestureRecognizer(target:self, action:#selector(self.handleSingleTap:))
singleTap.numberOfTouchesRequired = 1
singleTap.addTarget(self, action:#selector(self.handleSingleTap))
view.userInteractionEnabled = true
view.addGestureRecognizer(singleTap)
}
//event handler
func handleSingleTap(sender:UITapGestureRecognizer){
print("tapped")
}
}