Home > DeveloperSection > Forums > How to recognize UITapGestureRecognizer to specific views in controller?
Ashish Pandey

Total Post:128

Posted on    October-17-2014 11:14 PM

 iPhone iPhone  Ios 

 1 Reply(s)
 924  View(s)
Rate this:
I have a menu controller that when it pops up has a collection view. The way it works is that when the user taps outside the collection view it should make the menu dismiss. The menu dismisses when you tap outside the collection view, but it also dismisses when you tap inside the collection view. I can't seem to get the gesture recognizer to only dismiss when it is not inside the collection view. I have looked around on here and google and I haven't found any solution yet that works. Here is the code for the function that handles the tap:

    @IBAction func handleTap(sender: MenuTapGestureRecognizer)
        if (sender.state == .Ended && sender.view == menuCollectionView)
        else if (sender.state == .Ended && sender.view == mainView)
            self.dismissViewControllerAnimated(true, completion: nil)


jayprakash sharma
jayprakash sharma

Total Post:117

Posted on    October-17-2014 11:15 PM

I suspect you have only attached the gesture recognizer to your mainView. This is fine, but you will want to use sender.locationInView(menuCollectionView) and check to see if the point is within the bounds of your collection view. This is done with the following:

if (CGRectContainsPoint(menuCollectionView.bounds, sender.locationInView(menuCollectionView))) {
  // do stuff.

Modified On Apr-12-2016 12:22:58 AM

Don't want to miss updates? Please click the below button!

Follow MindStick