检测当前视图外的任何水龙头 [英] Detect any tap outside the current view

查看:96
本文介绍了检测当前视图外的任何水龙头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测当前视图外的任何水龙头?我试图用hitTest方法实现某些功能但未成功,但我不确定是否理解它。

Is there a way to detect any tap outside the current view? I unsuccessfully tried to implement something with the hitTest method but I am not sure to understand it well.

推荐答案

你需要做什么是,在 touchesBegan 中,您必须从触摸设置中获取第一个触摸对象,您必须在视图中检查该触摸的位置(您要在其中检测触摸)。

What you have to do is, In touchesBegan you have to get the first touch object from the touches set and you have to check the location of that touch in a view(inside which you want to detect the touch).

在View中获得触摸位置后,您必须检查 currentView (您需要查看的视图)检查水龙头是在里面还是外面。。

After you get the location of touch in View, you have to check whether your currentView(The view which you have to check whether tap was inside or outside).

如果currentView的框架包含触摸位置,则表示在视图内部发生了触摸,否则在外面。

If currentView's frame contains the touch location, that means touch has occurred inside the view otherwise outside.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        guard let location = touch?.location(in: self.view) else { return }
        if !currentView.frame.contains(location) {
            print("Tapped outside the view")
        }else {
            print("Tapped inside the view")
        }
    }

希望有帮助!

这篇关于检测当前视图外的任何水龙头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆