|
import UIKit |
|
// MARK: - UIButton Related |
|
public extension UIButton { |
|
private struct cs_associatedKeys { |
|
static var accpetEventInterval = "cs_acceptEventInterval" |
|
static var acceptEventTime = "cs_acceptEventTime" |
|
} |
|
// 重复点击的间隔 |
|
var cs_accpetEventInterval: NSTimeInterval { |
|
get { |
|
if let accpetEventInterval = objc_getAssociatedObject(self, &cs_associatedKeys.accpetEventInterval) as? NSTimeInterval { |
|
return accpetEventInterval |
|
} |
|
return 1.0 |
|
} |
|
set { |
|
objc_setAssociatedObject(self, &cs_associatedKeys.accpetEventInterval, newValue as NSTimeInterval, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) |
|
} |
|
} |
|
var cs_acceptEventTime: NSTimeInterval { |
|
get { |
|
if let acceptEventTime = objc_getAssociatedObject(self, &cs_associatedKeys.acceptEventTime) as? NSTimeInterval { |
|
return acceptEventTime |
|
} |
|
return 1.0 |
|
} |
|
set { |
|
objc_setAssociatedObject(self, &cs_associatedKeys.acceptEventTime, newValue as NSTimeInterval, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) |
|
} |
|
} |
|
override public class func initialize() { |
|
let before: Method = class_getInstanceMethod(self, #selector(UIButton.sendAction(_:to:forEvent:))) |
|
let after: Method = class_getInstanceMethod(self, #selector(UIButton.cs_sendAction(_:to:forEvent:))) |
|
method_exchangeImplementations(before, after) |
|
} |
|
func cs_sendAction(action: Selector, to target: AnyObject?, forEvent event: UIEvent?) { |
|
if NSDate().timeIntervalSince1970 - self.cs_acceptEventTime < self.cs_accpetEventInterval { |
|
return |
|
} |
|
if self.cs_accpetEventInterval > 0 { |
|
self.cs_acceptEventTime = NSDate().timeIntervalSince1970 |
|
} |
|
self.cs_sendAction(action, to: target, forEvent: event) |
|
} |
|
} |