UIGestureRecognizer blocks subview for handling touch events

后端 未结 10 1046
北恋
北恋 2020-11-28 19:32

I\'m trying to figure out how this is done the right way. I\'ve tried to depict the situation: \"enter

10条回答
  •  温柔的废话
    2020-11-28 19:49

    Building on @Pin Shih Wang answer. We ignore all taps other than those on the view containing the tap gesture recognizer. All taps are forwarded to the view hierarchy as normal as we've set tapGestureRecognizer.cancelsTouchesInView = false. Here is the code in Swift3/4:

    func ensureBackgroundTapDismissesKeyboard() {
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        tapGestureRecognizer.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tapGestureRecognizer)
    }
    
    @objc func handleTap(recognizer: UIGestureRecognizer) {
        let location = recognizer.location(in: self.view)
        let hitTestView = self.view.hitTest(location, with: UIEvent())
        if hitTestView?.gestureRecognizers?.contains(recognizer) == .some(true) {
            // I dismiss the keyboard on a tap on the scroll view
            // REPLACE with own logic
            self.view.endEditing(true)
        }
    }
    

提交回复
热议问题