How to recognize UITapGestureRecognizer to specific views in controller?

Total Post:128

Points:898
iPhone  Ios 
 1510  View(s)
Ratings:
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)
        {
            NSLog("menuCollectionView")
        }
        else if (sender.state == .Ended && sender.view == mainView)
        {
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
  1. Post:117

    Points:821
    Re: How to recognize UITapGestureRecognizer to specific views in controller?

    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-06-2018 12:55:37 AM

Answer