Created
August 8, 2015 04:47
-
-
Save JessyCatterwaul/c40b3e81cf6180e561fe to your computer and use it in GitHub Desktop.
The multiclosures get released before "event" returns; I need to figure out why, or another method to keep them around.
This file contains hidden or 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
extension UIControl { | |
override public func didMoveToSuperview() | |
{ | |
super.didMoveToSuperview() | |
alterControlEventsAssignment(addTarget) | |
} | |
override public func removeFromSuperview() | |
{ | |
super.removeFromSuperview() | |
alterControlEventsAssignment(removeTarget) | |
} | |
static let controlEvents = [ | |
HandlerSelector.onDidTouchDown.rawValue: UIControlEvents.TouchDown, | |
HandlerSelector.onValueChanged.rawValue: UIControlEvents.ValueChanged | |
] | |
private func alterControlEventsAssignment( | |
assign: ( | |
target: AnyObject?, | |
action: Selector, | |
forControlEvents: UIControlEvents | |
) -> () | |
) { | |
UIControl.controlEvents.forEach {assign( | |
target: self, | |
action: $0.0, | |
forControlEvents: $0.1 | |
)} | |
} | |
private func event(inout 😾events: [UIControl: MultiClosure<(), ()>]) | |
-> MultiClosure<(), ()> { | |
if let event = 😾events[self] {return event} | |
😾events[self] = MultiClosure<(), ()>() | |
return 😾events[self]! | |
} | |
var didTouchDown: MultiClosure<(), ()> {return event(&😾didTouchDown)} | |
var valueChanged: MultiClosure<(), ()> {return event(&😾valueChanged)} | |
func onDidTouchDown() {didTouchDown.run()} | |
func onValueChanged() {valueChanged.run()} | |
} | |
private var | |
😾didTouchDown = [UIControl: MultiClosure<(), ()>](), | |
😾valueChanged = [UIControl: MultiClosure<(), ()>]() | |
private extension UIControl {enum HandlerSelector: Selector { | |
case onDidTouchDown, onValueChanged | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment