Gesture Recognizer in Part 4

Hi group,

I am having trouble understanding this bit of code in a modal popup view controller called DetailViewController:

extension DetailViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
 return (touch.view === self.view)

let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("close")) 
gestureRecognizer.cancelsTouchesInView = false gestureRecognizer.delegate = self 

Shouldn’t touch.view === self.view make it so that a tap anywhere in the modal view would dismiss the view controller? How does the app know that the pop-up section being tapped should not dismiss the view?

Here, self.view refers to the view of the entire DetailViewController, which fills up the whole screen. The little pop-up panel with the image and labels is a subview of that self.view.

Since we do touch.view === self.view, we only test whether the touch was on the background part. If you touch a label or a button or the image, then touch.view points at that label, button, or image and not at self.view.

What you describe would be more like touch.view == self.view || self.view.subviews.contains(touch.view). But that’s not what we’re doing here. :slightly_smiling: