Scott Kilbourn
Scott Kilbourn

Reputation: 1586

UITapGestureRecognizer responding to incorrect UIView

I have a 100x600 UIView sitting on top of the main UIView in a ViewController. Normally, I want to keep this view hidden. When the user taps on one of several controls, including the main view, I unhide the sub UIView. Tapping anywhere on the main UIView will hide it.

In viewDidLoad, I have the following UITapGestureRecognizer:

    let viewTouch = UITapGestureRecognizer(target: self, action: "viewClicked:")
    self.view.addGestureRecognizer(viewTouch)

The viewClicked function looks like this:

func viewClicked(gestureRecognizer: UIGestureRecognizer) {
    handleDisplayOfDropdownView()
}

The handleDisplayOfDropdown function is responsible for showing and hiding the sub UIView. All it does is adjust the alpha of the subview, so that it is either visible or not visible.

func handleDisplayOfDropdownView() {
    UIView.animateWithDuration(0.5, animations: {
        if !self.dropDownVisible {
            self.dropDownVisible = true
            self.dropDownView.alpha = 1.0
        }
        else {
            self.dropDownVisible = false
            self.dropDownView.alpha = 0
        }
    })
}

All of the UI work is done by storyboard. I just have a 100x600 view placed at the top of the ViewController. It is made visible and hidden by tapping on the main UIView.

The problem that I'm having is that the sub UIView disappears when I tap on it. When I set up the UITapGestureRecognizer, I figured that only a tap on the main UIView would activate and hide it. It seems that tapping anywhere, including the sub UIView is hiding it.

Am I setting up the UITapGestureRecognizer incorrectly?

Upvotes: 1

Views: 885

Answers (1)

Hamza Ansari
Hamza Ansari

Reputation: 3074

Add gestureDelegate:

 UIGestureRecognizerDelegate

In ViewDidLoad set viewTouch UITapGesture delegate:

viewTouch.delegate = self

Then call this delegate

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    let p = touch.locationInView(view)
    if CGRectContainsPoint(dropDownView.frame, p) {
      return false
    }
    return true
  }

Upvotes: 2

Related Questions