触摸开始消除视图

发布于 2025-01-15 22:31:15 字数 702 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2025-01-22 22:31:15

您应该检查触摸是否不在视图中,而不是检查不同的

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    let location = touch.location(in: self.view)
    
    if (!viewForVc.bounds.contains(location)) {
        EditTodoViewController.textFromCellForTodo = ""
        dismiss(animated: true)
    }
}

--- 编辑----

如果您想在检查包含之前以编程方式更改框架,请检查下面的代码

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        let location = touch.location(in: self.view)
        
        // get the frame of view + 10% height
        let viewFrame = CGRect(origin: viewForVc.frame.origin, size: CGSize(width: viewForVc.frame.size.width, height: viewForVc.frame.size.height * 1.1))
        
        if (!viewFrame.contains(location)) {
            EditTodoViewController.textFromCellForTodo = ""
            dismiss(animated: true)
        }
    }

You should check if the touch is not in the view rather than check different

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    let location = touch.location(in: self.view)
    
    if (!viewForVc.bounds.contains(location)) {
        EditTodoViewController.textFromCellForTodo = ""
        dismiss(animated: true)
    }
}

--- Edit----

If you want to change the frame programmatically before check contains check the code below

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        let location = touch.location(in: self.view)
        
        // get the frame of view + 10% height
        let viewFrame = CGRect(origin: viewForVc.frame.origin, size: CGSize(width: viewForVc.frame.size.width, height: viewForVc.frame.size.height * 1.1))
        
        if (!viewFrame.contains(location)) {
            EditTodoViewController.textFromCellForTodo = ""
            dismiss(animated: true)
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文