Restructure

This commit is contained in:
Zhigang Fang
2016-03-20 11:52:06 +08:00
parent 4a90722996
commit 32793d2eef
8 changed files with 1003 additions and 525 deletions

View File

@@ -13,19 +13,19 @@ public protocol Onable {
}
class DummyTarget: NSObject {
private class DummyTarget: NSObject {
let action:UIControlEvents -> Void
init(action:UIControlEvents -> Void) {
self.action = action
}
func execute(event:UIControlEvents) {
@objc func execute(event:UIControlEvents) {
self.action(event)
}
}
private var callbackStoreKey:UInt8 = 0
extension UIControl {
private extension UIControl {
var callbacks:[UInt: DummyTarget] {
get {
return objc_getAssociatedObject(self, &callbackStoreKey) as? [UInt: DummyTarget] ?? [:]
@@ -39,14 +39,14 @@ extension UIControl {
extension UIControl : Onable {}
extension Onable where Self : UIControl {
func on(event:UIControlEvents, action:(Self, UIControlEvents) -> Void) {
public func on(event:UIControlEvents, action:(Self, UIControlEvents) -> Void) {
let dummyTarget = DummyTarget(action: {
action(self, $0)
})
self.addTarget(dummyTarget, action: #selector(DummyTarget.execute(_:)), forControlEvents: event)
self.callbacks[event.rawValue] = dummyTarget
}
func off(event:UIControlEvents) {
public func off(event:UIControlEvents) {
guard let dummyTarget = self.callbacks[event.rawValue] else { return }
self.removeTarget(dummyTarget, action: #selector(DummyTarget.execute(_:)), forControlEvents: event)
self.callbacks[event.rawValue] = nil

File diff suppressed because it is too large Load Diff