Skip to content

Instantly share code, notes, and snippets.

@mttcrsp
Last active May 4, 2022 20:44
Show Gist options
  • Save mttcrsp/2069fe4489306a1dd3df8630e4b618d2 to your computer and use it in GitHub Desktop.
Save mttcrsp/2069fe4489306a1dd3df8630e4b618d2 to your computer and use it in GitHub Desktop.
Sample app that demoes events flowing through the responder chain
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = Window()
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
@objc func `do`(_ sender: Any, _ event: UIEvent?) {
print(Self.self, #function, sender, event as Any)
}
}
@objc class Window: UIWindow {
@objc func `do`(_ sender: Any, _ event: UIEvent?) {
print(Self.self, #function, sender, event as Any)
}
// override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
// print(action, sender as Any)
// return false
// }
}
@objc class ViewController: UIViewController {
override func loadView() {
view = Control(frame: .zero)
}
// @objc func `do`(_ sender: Any, _ event: UIEvent?) {
// print(Self.self, #function, sender, event as Any)
// }
}
@objc class Control: UIControl {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .systemBackground
let tapAction = #selector(tapped)
let tapRecognizer = UITapGestureRecognizer(target: self, action: tapAction)
addGestureRecognizer(tapRecognizer)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func tapped(_ event: UIEvent?) {
sendAction(Selector("do::"), to: nil, for: UIEvent())
}
// @objc func `do`(_ sender: Any, _ event: UIEvent?) {
// print(Self.self, #function, sender, event as Any)
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment