Visual_Novel_iOS/crush/Crush/Src/Utils/Extensions/UIButtonExt.swift

87 lines
3.1 KiB
Swift
Raw Normal View History

2025-10-09 10:29:35 +00:00
//
// UIButtonExt.swift
// Crush
//
// Created by Leon on 2025/7/18.
//
extension UIButton {
// 使 associated object
private struct AssociatedKeys {
static var touchAreaInsets = "touchAreaInsets"
}
//
@objc var touchAreaInsets: UIEdgeInsets {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.touchAreaInsets) as? UIEdgeInsets ?? .zero
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.touchAreaInsets, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
// point(inside:with:)
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let insets = touchAreaInsets
//
let extendedBounds = bounds.inset(by: UIEdgeInsets(
top: -insets.top,
left: -insets.left,
bottom: -insets.bottom,
right: -insets.right
))
return extendedBounds.contains(point)
}
}
// barButton
//import ObjectiveC.runtime
//
//extension UIControl {
// private struct AssociatedKeys {
// static var ignoreEvent = 0
// static var clickInterval = 0
// }
//
// ///
// private var isIgnoringEvent: Bool {
// get { (objc_getAssociatedObject(self, &AssociatedKeys.ignoreEvent) as? Bool) ?? false }
// set { objc_setAssociatedObject(self, &AssociatedKeys.ignoreEvent, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
// }
//
// /// 0.8
// var clickInterval: TimeInterval {
// get { (objc_getAssociatedObject(self, &AssociatedKeys.clickInterval) as? TimeInterval) ?? 0.8 }
// set { objc_setAssociatedObject(self, &AssociatedKeys.clickInterval, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
// }
//
// // MARK: -
// public static func enableClickDebounce() {
// let originalSelector = #selector(UIControl.sendAction(_:to:for:))
// let swizzledSelector = #selector(UIControl.swizzled_sendAction(_:to:for:))
//
// guard let originalMethod = class_getInstanceMethod(UIControl.self, originalSelector),
// let swizzledMethod = class_getInstanceMethod(UIControl.self, swizzledSelector) else {
// return
// }
// method_exchangeImplementations(originalMethod, swizzledMethod)
// }
//
// @objc private func swizzled_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
// }
// }
//
// //
// swizzled_sendAction(action, to: target, for: event)
// }
//}