UITapGestureRecognizer tap on self.view but ignore subviews

后端 未结 12 798
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 21:49

I need to implement a feature that will invoke some code when I double tap on the self.view (view of UIViewCotroller). But the problem that I have other UI obje

12条回答
  •  鱼传尺愫
    2020-11-30 22:20

    Note that the gestureRecognizer API has changed to:

    gestureRecognizer(_:shouldReceive:)

    Take particular note of the underscore (skip) indicator for the first parameter's external label.

    Using many of the examples provided above, I was not receiving the event. Below is a an example that works for current versions of Swift (3+).

    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        var shouldReceive = false
        if let clickedView = touch.view {
            if clickedView == self.view {
                shouldReceive = true;
            }
        }
        return shouldReceive
    }
    

提交回复
热议问题