This question already has an answer here:
- How to detect all touches in Swift 2 2 answers
What code can be used to detect when a user is interacting with an app and when the user is not interacting with an app?
Currently I have a ViewController
with a UIView
that is receiving touches by overriding touches, also by receiving panning gestures and tap gestures. A solution to this problem needs to be separate to those current gestures or sit above those gestures.
Is there a gesture recognizer that sits above everything else that can tell my app when it received gestures and when there is no gesture received?
When the app is active, is there a way to monitor if the app is receiving touches and when it isn't and call a function as required, for example:
func appActive(){
print("App received input from a touch, tap, swipe, long press etc.")
}
func appInactive(){
print("App stopped receiving any input.")
}
Thank you.
1 - Locate your project's Swift file
AppDelegate.swift
, and comment out@UIApplicationMain
:2 - Add a new Swift file to your project named exactly
main.swift
, and add the code:3 - Add a new Swift file to your project named exactly
UIApplication.swift
, and add the code:4 - Locate your project's
Info.plist
file, and add a new key (Xcode menu:Editor > Add Item
), select or type keyPrincipal class
, with string valueMyApplication
.5 - Run your project!
A way to intercept any touches of your application is to create a custom UIWindow which will catch the touches without canceling them.
You have to add this window in your Application Delegate, and set its level above the main window.