I'm trying to implement a lock screen in the SwiftUI app.
I need to track every event in order to restart the lock timer.
In UIKit app, I used this approach - overriding UIApplication, which allows being aware of any event across the app:
override func sendEvent(_ event: UIEvent) {
super.sendEvent(event)
switch event.type {
case .touches:
// Post Notification or Delegate here
default:
break
}
}
But in SwiftUI it is not supported anymore. I tried to add
.onTapGesture {}
to the root ContentView, but it doesn't work as expected.
Is there any way to avoid adding
.onTapGesture {}
to every single view in the app?
Here is a possible solution:
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear(perform: UIApplication.shared.addTapGestureRecognizer)
}
}
}
extension UIApplication {
func addTapGestureRecognizer() {
guard let window = windows.first else { return }
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapAction))
tapGesture.requiresExclusiveTouchType = false
tapGesture.cancelsTouchesInView = false
tapGesture.delegate = self
window.addGestureRecognizer(tapGesture)
}
@objc func tapAction(_ sender: UITapGestureRecognizer) {
print("tapped")
}
}
extension UIApplication: UIGestureRecognizerDelegate {
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true // set to `false` if you don't want to detect tap during other gestures
}
}
Based on pawello2222's solution , I added UIPanGestureRecognizer to be able to recognize when scrolling or swiping around the app too. Also added a notification observer to recognize when typing text from keyboard. The solution would look like this:
@main
struct YourApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear {
// Interaction recognizer implementation
UIApplication.shared.addInteractionRecognizer()
}
}
}
}
extension UIApplication {
func addInteractionRecognizer() {
// Notification observer to track text changes from keyboard
NotificationCenter.default.addObserver(self, selector: #selector(didInteractWithApp), name: UITextField.textDidChangeNotification, object: nil)
guard let window = windows.first else { return }
// Gestures recognizers to track
let gestureRecognizers = [
UITapGestureRecognizer(target: self, action: #selector(didInteractWithApp)),
UIPanGestureRecognizer(target: self, action: #selector(didInteractWithApp))
]
gestureRecognizers.forEach {
$0.requiresExclusiveTouchType = false
$0.cancelsTouchesInView = false
$0.delegate = self
window.addGestureRecognizer($0)
}
}
@objc func didInteractWithKeyboard() {
// Restart the lock timer
}
@objc func didInteractWithApp(_ sender: UIGestureRecognizer) {
// Optional: Validate UIPanGestureRecognizer has ended, cancelled or failed, to prevent overloading for restarting timer. Remove if not needed
let allowedStates: [UIGestureRecognizer.State] = [.ended, .cancelled, .failed]
if sender as? UIPanGestureRecognizer != nil, !allowedStates.contains(sender.state) {
return
}
// Restart the lock timer
}
}
extension UIApplication: UIGestureRecognizerDelegate {
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Set to true to recognize gestures specified above while allowing user interact with other gestures in the app and not to block them with them
return true
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.