Skip to content

Instantly share code, notes, and snippets.

@SURYAKANTSHARMA
Last active February 15, 2020 12:59
Show Gist options
  • Save SURYAKANTSHARMA/dcedcd51849cf0c5c09df704beff399e to your computer and use it in GitHub Desktop.
Save SURYAKANTSHARMA/dcedcd51849cf0c5c09df704beff399e to your computer and use it in GitHub Desktop.
typealias CGFloat = (Float, Float)
class UIResponder {
func handleTap(event: TapEvent) {}
}
class TapEvent {
let location: CGFloat
init(location: CGFloat) {
self.location = location
}
}
class UIView: UIResponder {
var successor: UIResponder? = nil
override func handleTap(event: TapEvent) {
guard canHandleTap() else {
successor?.handleTap(event: event)
return
}
print("Tap handled by view \(event.location)")
}
func canHandleTap() -> Bool { return false }
}
class UITableView: UIResponder {
var successor: UIResponder?
override func handleTap(event: TapEvent) {
guard canHandleTap() else {
successor?.handleTap(event: event)
return
}
print("Tap handled by UITableView \(event.location)")
}
func canHandleTap() -> Bool { return false }
}
class UIButton: UIResponder {
var successor: UIResponder?
override func handleTap(event: TapEvent) {
guard canHandleTap() else {
successor?.handleTap(event: event)
return
}
print("Tap handled by Button \(event.location)")
}
func canHandleTap() -> Bool { return true }
}
let button = UIButton()
button.successor = nil
let tableView = UITableView()
tableView.successor = button
let view = UIView()
view.successor = tableView
let tapEvent = TapEvent(location: (10.0,10.0))
view.handleTap(event: tapEvent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment