Skip to content

如何全局添加点击事件(所有点击都会触发) #73

Open

Description

@resse92
  1. 继承一个来自于UITapGestureRecognizer的子类,重写touchesBegan等方法:
import UIKit
import UIKit.UIGestureRecognizerSubclass
class AllTouchesGestureRecognizer: UITapGestureRecognizer {
    static var shouldPlaySoundEffect = UserDefaults.Doll.soundEffect.storedBool
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesBegan(touches, with: event)
        // do everything what you want
    }
}

然后在Appdelegate中加入到window中:

let reco = AllTouchesGestureRecognizer.init(target: nil, action: nil)
    reco.cancelsTouchesInView = false
    self.window?.addGestureRecognizer(reco)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions