我有一个显示为 modalPresentationStyle = .overCurrentContext
的 UIViewController。
这个上下文 View (self.view
) 有 backgroundColor = .clear
和一个名为 content
的 subview 。
content
是全边界宽度和半边界高度,背景为白色。
我已将 UITapGestureRecognizer 添加到 self.view
,但我不知道在点击重叠 View (content
) 时不要触发点击操作。
关于如何仅在用户点击 View 而不是 subview 时触发操作的想法?
最佳答案
你需要遵循UIGestureRecognizerDelegate
,然后实现shouldReceive touch
委托(delegate)方法:
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if let touchView = touch.view {
if touchView.isDescendant(of: view) {
return false
}
}
return true
}
}
所以如果触摸区域是subView,那么点击手势将被忽略。
关于swift - UITapGestureRecognizer : Modal View is overlapped by another view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54374633/