I have a main viewController, it is called WelcomeViewController
. I have a UIView subclass and that has some view related stuff in it. I want to add a UITapGestureRecognizer
to that subclass. I only want the gesture recognizer to acknowledge taps inside that subview. How do I do that. Should I put the UITapGestureRecognizer
in the subclass or should I put it in the Welcome vc. Thanks in advance.
Also, I have played around with it a bunch and can't seem to figure it out.
UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer . For gesture recognition, the specified number of fingers must tap the view a specified number of times. Although taps are discrete gestures, they're discrete for each state of the gesture recognizer.
Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.
It depends on whether you want to handle the tap in your custom view object or in the view controller.
If in the view, add this to it's init
or other proper place:
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
If in the view controller, add this in the viewDidLoad
(or other proper place):
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[yourCustomView addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
handler is the same:
- (void)handleTap:(UITapGestureRecognizer*)recognizer
{
// Do Your thing.
if (recognizer.state == UIGestureRecognizerStateEnded)
{
}
}
Take a look at the SimpleGestureRecognizers example and you should get a pretty good idea.
---- Updated 10/1/2012----
For those of you who like to use storyboard/nib, this is super simple!
Open your storyboard/nib.
Drag-b-drop the kind of recognizer you want from the Object Library onto the UI element you want.
Right-click on the recognizer object, then connect its selector
to an IBAction in the File's Owner (usually an UIViewController.) If you need to, connect the delegate as well.
You're done!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With