首页 > 解决方案 > 当用户触摸屏幕的特定部分时如何触发事件 - ios?

问题描述

我有一个使用 UIViewController 制作的自定义弹出窗口(演示文稿:'over current context') - 请参见下文。我想做的是一旦用户触摸屏幕的顶部 - 黑暗的一半,它不包含菜单选项,就会关闭 UIViewController 。我可以触发什么事件来让它工作?

在此处输入图像描述

标签: iosswift

解决方案


您可以将轻击手势添加到 darkView 并在其中执行

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
mydarkView.addGestureRecognizer(tapGesture)

//

// 3. this method is called when a tap is recognized
@objc func handleTap(_ sender: UITapGestureRecognizer) {
     self.dismiss(animated:true,completion:true)
}

使用

override func touchesBegan(_ touches: Set<UITouch>, 
         with event: UIEvent?)

推荐阅读