且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我们如何检测 iOS 7 上的控制中心?

更新时间:2023-02-03 09:56:29

我刚刚发布了一个小 UIWindow 子类,它正是这样做的.您只需订阅 NSNotification 并可以对打开控制中心的用户做出反应.Github 上的详细说明和设置:AAWindow.

I just published a little UIWindow subclass that does exactly that. You simply subscribe to an NSNotification and can react to the user opening Control Center. Detailed instructions and setup on Github: AAWindow.

实现这一点的方法是结合使用 NSTimer 和覆盖 UIWindow 中的 sendEvent 来接收所有触摸而不阻止它们.因此,您基本上会收到所有触摸,检查它们是否靠近屏幕的下边缘,如果是,则将计时器设置为半秒,并且如果在此计时器运行期间调用 applicationWillResignActive,您几乎可以确定 ControlCenter 已打开.如果没有状态栏,时间必须有所不同,因为那时应用程序处于全屏状态,用户可能需要长达 3 秒才能启动控制中心.

The way this is accomplished is by using a combination of NSTimer and overwriting sendEvent in UIWindow to receive all touches without blocking them. So you basically receive all touches check if they are near the lower edge of the screen, if yes set a timer for a half a second and if during this timer is running applicationWillResignActive is called you can be almost certain that ControlCenter is opened. The time has to vary if there's no statusbar, because then the app is in fullscreen and it can take the user up to 3 seconds to launch Control Center.