Last active
February 15, 2020 12:59
-
-
Save SURYAKANTSHARMA/dcedcd51849cf0c5c09df704beff399e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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