58 lines
1.4 KiB
Swift
58 lines
1.4 KiB
Swift
|
|
//
|
|||
|
|
// CLButton.swift
|
|||
|
|
// Crush
|
|||
|
|
//
|
|||
|
|
// Created by Leon on 2025/9/25.
|
|||
|
|
//
|
|||
|
|
|
|||
|
|
import UIKit
|
|||
|
|
|
|||
|
|
class CLButton: UIButton {
|
|||
|
|
|
|||
|
|
/// 点击间隔(秒),默认 0.8s
|
|||
|
|
var clickInterval: TimeInterval = 0.8
|
|||
|
|
|
|||
|
|
private var isIgnoringEvent = false
|
|||
|
|
|
|||
|
|
override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
|
|||
|
|
if isIgnoringEvent {
|
|||
|
|
// 正在防抖,丢弃点击
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if clickInterval > 0 {
|
|||
|
|
isIgnoringEvent = true
|
|||
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + clickInterval) { [weak self] in
|
|||
|
|
self?.isIgnoringEvent = false
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
super.sendAction(action, to: target, for: event)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
class CLControl: UIControl {
|
|||
|
|
|
|||
|
|
/// 点击间隔(秒),默认 0.8s
|
|||
|
|
@IBInspectable var clickInterval: TimeInterval = 0.8
|
|||
|
|
|
|||
|
|
private var isIgnoringEvent = false
|
|||
|
|
|
|||
|
|
override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
|
|||
|
|
if isIgnoringEvent {
|
|||
|
|
// 正在防抖,丢弃点击
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if clickInterval > 0 {
|
|||
|
|
isIgnoringEvent = true
|
|||
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + clickInterval) { [weak self] in
|
|||
|
|
self?.isIgnoringEvent = false
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
super.sendAction(action, to: target, for: event)
|
|||
|
|
}
|
|||
|
|
}
|