如何在SwiftUI应用程序中跟踪所有触摸

wfveoks0  于 5个月前  发布在  Swift
关注(0)|答案(2)|浏览(42)

我正在尝试在SwiftUI应用程序中实现锁定屏幕。
我需要跟踪每一个事件,以便重新启动锁定计时器。
在UIKit应用程序中,我使用了这种方法-覆盖UIApplication,它允许感知应用程序中的任何事件:

override func sendEvent(_ event: UIEvent) {
  super.sendEvent(event)

  switch event.type {
  case .touches:
    // Post Notification or Delegate here
  default:
    break
  }
}

字符串
但是在SwiftUI中它不再被支持了。

.onTapGesture {}


到根ContentView,但它并不像预期的那样工作。
有没有办法避免增加

.onTapGesture {}


应用程序中的每一个视图

mzaanser

mzaanser1#

以下是一个可能的解决方案:

@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
    }
}

字符串

xoshrz7s

xoshrz7s2#

基于pawello2222's solution,我添加了UIPanGestureRecognizer,以便在滚动或滑动应用时也能够识别。还添加了一个通知观察器,以便在从键盘输入文本时识别。解决方案如下所示:

@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(didInteractWithKeyboard), 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
    }
}

字符串

相关问题