7

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.

beryllium
  • 29,669
  • 15
  • 106
  • 125
Andrew
  • 3,874
  • 5
  • 39
  • 67

5 Answers5

18

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!

  1. Open your storyboard/nib.

  2. Drag-b-drop the kind of recognizer you want from the Object Library onto the UI element you want.

  3. 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.

  4. You're done!enter image description here

Joseph Lin
  • 3,324
  • 1
  • 29
  • 39
  • I'm good with all that. The problem is I subclassed a UIView into my main view. Now, which view do I add the UITapGestureRecognizer to. The subview (which isn't working) or the main view (which makes it work when I tap anywhere, including outside the subview) – Andrew May 10 '11 at 19:11
  • I've refined my answer. Does that make more sense to your situation? – Joseph Lin May 10 '11 at 19:20
  • Your main view might not pass events to subview. Also, you can check if touch point is in subview frame. – TheBlack May 10 '11 at 21:22
  • How can I check if the touch point is in the subview. I think I will just do that and then call the method in the other class from that. – Andrew May 10 '11 at 23:28
  • Thanks for the edit. That helps, but I would prefer to handle the gesture from the custom view. If that is not possible I can just call the method in the custom view from the viwController – Andrew May 10 '11 at 23:33
  • That's the first part of my answer. Just add that to your custom view's init method. – Joseph Lin May 11 '11 at 16:53
  • If you decide to put the code into the `init` method of your subclass of `UIView` be sure to override the correct init method. If your View is instantiated inside of IB you need to override `initWithCoder:`, if you instantiate it programmatically you need to override `initWithFrame`. See this [SO-Answer](http://stackoverflow.com/a/12202213/641514). – Aufwind Aug 30 '12 at 17:30
  • thanks @JosephLin with a very small amount of tweaking your solution worked for me with a text view in the viewDidLoad. I was trying to figure out how do that via the storyboard in iOS 6 but it was not obvious. Your simple solution just works. :D – sdjuan Oct 01 '12 at 05:45
  • 1
    @sdjuan, adding a gesture recognizer in the storyboard is even simpler! I've updated my post a bit to include that. Hope that helps! – Joseph Lin Oct 01 '12 at 19:06
2

I think you need to set the delegate of the gesture recognizer to be whatever view controller you want to handle the taps.

gestureRecognizer.delegate = self;

for example. Then adopt the UIGestureRecognizerDelegate protocol in your header.

Jamie
  • 5,090
  • 28
  • 26
1

Please check view.userInteractionEnabled=true;

1

You can choose the receiver of the UITapGestureRecognizer, it doesn't have to be the same class that instantiated the recognizer (i.e. self). If you create it in your WelcomeViewConroller, you can select any subview to receive the events:

[tapRecognizer addTarget:aSubview action:@selector(myMethod:)];
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
1

I think you may have the same problem I experienced. When you call [yourCustomView addGestureRecognizer:tapRecognizer]; you need to reference via a UIView * so in your example try:

UIView *mySubView = yourCustomView; 
[mySubView addGestureRecognizer:tapRecognizer];

hope this helps.

kevj
  • 87
  • 2
  • 7