亲测多个按钮点击互不干扰


- 使用
btn.addAction { // 不加延迟时间
print("点击按钮")
}
btn.addAction(3) { [weak self] in // 延迟3秒才可点击
self?.label.text = "\(Date())"
print("点击时间\(Date())")
}
btn1.addAction(10) { [weak self] in // 延迟10秒才可点击
self?.label1.text = "\(Date())"
print("点击时间\(Date())")
}
- 核心代码
// MARK: - 快速设置按钮 并监听点击事件
typealias buttonClick = (()->()) // 定义数据类型(其实就是设置别名)
extension UIButton {
// 改进写法【推荐】
private struct HWRuntimeKey {
static let actionBlock = UnsafeRawPointer.init(bitPattern: "actionBlock".hashValue)
static let delay = UnsafeRawPointer.init(bitPattern: "delay".hashValue)
/// ...其他Key声明
}
/// 运行时关联
private var actionBlock: buttonClick? {
set {
objc_setAssociatedObject(self, UIButton.HWRuntimeKey.actionBlock!, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
}
get {
return objc_getAssociatedObject(self, UIButton.HWRuntimeKey.actionBlock!) as? buttonClick
}
}
private var delay: TimeInterval {
set {
objc_setAssociatedObject(self, UIButton.HWRuntimeKey.delay!, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
}
get {
return objc_getAssociatedObject(self, UIButton.HWRuntimeKey.delay!) as? TimeInterval ?? 0
}
}
/// 点击回调
@objc private func tapped(button:UIButton) {
actionBlock?()
isEnabled = false
// 4.GCD 主线程/子线程
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self] in // 延迟调用方法
DispatchQueue.main.async { // 不知道有没有用反正写上就对了
print("恢复时间\(Date())")
self?.isEnabled = true
}
}
}
/// 添加点击事件
func addAction(_ delay: TimeInterval = 0, action:@escaping buttonClick) {
addTarget(self, action:#selector(tapped(button:)) , for:.touchUpInside)
self.delay = delay
actionBlock = action
}
}
网友评论