触摸开始消除视图
我的 ViewController 中有一个视图,它占据了从底部到中间屏幕的一半屏幕。我正在使用 TouchBegan 函数来关闭视图,如果在视图内部以外的其他地方点击,一切都会正常,除了当我触摸视图顶部的 10% 时,它会在不应该关闭的时候关闭,因为触摸仍在视图内允许查看。
这是我展示 vc 的代码:
guard let vc = storyboard?.instantiateViewController(withIdentifier: "editvc") as? EditTodoViewController else {return}
vc.modalPresentationStyle = .custom
present(vc, animated: true)
这是我在该 vc 中的函数:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if touch != viewForVc {
EditTodoViewController.textFromCellForTodo = ""
dismiss(animated: true)
}
}
I have a view in my ViewController that takes up half the screen from bottom to mid-screen. I'm using touchesBegan function to dismiss the view if tapped somewhere else than inside the view, everything works except that when I touch on the 10% of the top on the view, it dismisses when it should not because the touch is still inside the allowed view.
This is my code to present the vc:
guard let vc = storyboard?.instantiateViewController(withIdentifier: "editvc") as? EditTodoViewController else {return}
vc.modalPresentationStyle = .custom
present(vc, animated: true)
and this is my function inside that vc:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if touch != viewForVc {
EditTodoViewController.textFromCellForTodo = ""
dismiss(animated: true)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该检查触摸是否不在视图中,而不是检查不同的
--- 编辑----
如果您想在检查包含之前以编程方式更改框架,请检查下面的代码
You should check if the touch is not in the view rather than check different
--- Edit----
If you want to change the frame programmatically before check contains check the code below