mirror of
https://github.com/zhigang1992/on.git
synced 2026-04-28 11:05:23 +08:00
Restructure
This commit is contained in:
@@ -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
Reference in New Issue
Block a user