Swift检测屏幕上任何位置的触摸 [英] Swift detect touch anywhere on the screen

查看:619
本文介绍了Swift检测屏幕上任何位置的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以使用touchesBegan,touchesEnded等来检测触摸。
这些都可以。唯一的问题是他们只检测视图本身的触摸,而不是像文本字段那样检测视图之上的任何内容。

I know you can detect touches using touchesBegan, touchesEnded, etc. These all work. The only thing is that they only detect the touches on the view itself, not on anything on top of that view like a text field.

我的目标是创建一个超时经过一段时间的不活动。如果触摸屏幕,则会重置超时,这意味着某人仍在使用该应用程序。它可以工作到目前为止,只要他们不点击任何控件(如标签,按钮,textview等)。

My goal is to create a timeout after a certain period of inactivity. The timeout will be reset if the screen is touched, meaning someone is still using the app. It works so far, as long as they don't tap on any controls (like a label, button, textview, etc).

我还可以重置超时任何控件都被点击,但这需要更多的情况(不同视图控制器上的不同类型的控件)。

I could also reset the timeout when any controls are tapped, but that would require a lot more cases (different types of controls on different view controllers).

我正在寻找并结束所有屏幕点击检测方法。有什么想法吗?

I'm looking for and end-all screen tap detection method. Any ideas?

推荐答案

你可以把它添加到AppDelegate:

You can add this to your AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let tapGesture = UITapGestureRecognizer(target: self, action: nil)
        tapGesture.delegate = self
        window?.addGestureRecognizer(tapGesture)

        return true
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
        // User tapped on screen, do whatever you want to do here.

        return false
}

并使AppDelegate符合规定到UIGestureRecognizerDelegate协议。

And also make your AppDelegate conform to UIGestureRecognizerDelegate protocol.

这篇关于Swift检测屏幕上任何位置的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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