代码之家  ›  专栏  ›  技术社区  ›  Cheok Yan Cheng

为什么当点击区域落在按钮上时,UIGestureRecognizerDelegate的shouldRecognizeSimultaneouslyWith没有执行?

  •  0
  • Cheok Yan Cheng  · 技术社区  · 5 年前

    我想

    1. 检测屏幕上任何位置的全局触摸事件
    2. 不会影响按钮和下面的自定义视图等组件。下面的按钮、自定义视图仍然能够接收点击事件。

    我所做的是

    1. UITapGestureRecognizer 在里面 UIApplication.shared.keyWindow
    2. UIGestureRecognizerDelegate ,返回 true 对于 shouldRecognizeSimultaneouslyWith

    这是我的密码

    import UIKit
    
    extension UIWindow {
        static var key: UIWindow! {
            if #available(iOS 13, *) {
                return UIApplication.shared.windows.first { $0.isKeyWindow }
            } else {
                return UIApplication.shared.keyWindow
            }
        }
    }
    
    extension ViewController: UIGestureRecognizerDelegate {
        func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
               shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer)
                -> Bool {
            print(">>> shouldRecognizeSimultaneouslyWith returns true")
            return true
        }
    }
    
    class ViewController: UIViewController {
        // Lazy is required as self is not ready yet without lazy.
        private lazy var globalGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(globalTapped))
        
        private func installGlobalGestureRecognizer() {
            UIWindow.key.removeGestureRecognizer(globalGestureRecognizer)
            UIWindow.key.addGestureRecognizer(globalGestureRecognizer)
            
            globalGestureRecognizer.delegate = self
        }
    
        @objc func globalTapped() {
            print(">>> global tapped")
        }
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }
    
        @IBAction func buttonClicked(_ sender: Any) {
            print("yellow button tap\n")
        }
        
        @IBAction func handleTap(_ gesture: UITapGestureRecognizer) {
            print("red view tap\n")
        }
        
        @IBAction func installButtonClicked(_ sender: Any) {
            print("install global gesture")
            installGlobalGestureRecognizer()
        }
    }
    

    这是点击红色自定义视图时发生的情况

    install global gesture
    >>> shouldRecognizeSimultaneouslyWith returns true
    >>> shouldRecognizeSimultaneouslyWith returns true
    >>> global tapped
    red view tap
    

    点击黄色按钮时(全局手势不工作)

    install global gesture
    yellow button tap
    

    这是我为自定义红色视图和黄色按钮安装tap事件处理程序的方式。

    enter image description here

    enter image description here

    有人知道为什么吗 点击按钮时是否未调用?我希望在点击黄色按钮时

    • 应同时识别 已执行并返回true
    • 已执行全局手势点击事件处理程序

    谢谢

    0 回复  |  直到 5 年前
        1
  •  3
  •   Phil Dukhov    5 年前

    正如@DonMag正确地说的那样,这种情况的发生是因为按钮触摸没有被处理 UIGestureRecognizer s

    UIControl 直接从 UIWindow UIApplication

    1. 使用 UI窗口 SceneDelegate
    class CustomWindow: UIWindow {
        override func sendEvent(_ event: UIEvent) {
            super.sendEvent(event)
            print(#function, event)
        }
    }
    
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
        var window: UIWindow?
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            guard let scene = (scene as? UIWindowScene) else { return }
            window = CustomWindow(windowScene: scene)
            window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
        }
    }
    
    1. 我更喜欢使用 UIApplication应用程序
    • UIApplication子类
    class CustomUIApplication: UIApplication {
        override func sendEvent(_ event: UIEvent) {
            super.sendEvent(event)
            print(#function, event)
        }
    }
    
    • 去除 @main 注释自 AppDelegate
    • main.swift 包括以下内容:
    UIApplicationMain(
        CommandLine.argc,
        CommandLine.unsafeArgv,
    
        NSStringFromClass(CustomUIApplication.self),
        NSStringFromClass(AppDelegate.self)
    )
    
        2
  •  0
  •   Wide Angle Technology    5 年前

        3
  •  0
  •   Nadeem Taj    5 年前

    原因是在 viewDidLoad() 您的 ViewController 视图控制器 .

    为了更好地理解 enter link description here