AutoCat/AutoCat/ACUIKit/Extensions/GestureRecognizers.swift

43 lines
1.1 KiB
Swift

//
// GestureRecognizers.swift
// AutoCat
//
// Created by Selim Mustafaev on 19.02.2023.
// Copyright © 2023 Selim Mustafaev. All rights reserved.
//
import UIKit
extension UIGestureRecognizer {
typealias Action = ((UIGestureRecognizer) -> ())
private struct Keys {
static var actionKey = "ActionKey"
}
private var block: Action? {
set {
if let newValue = newValue {
// Computed properties get stored as associated objects
objc_setAssociatedObject(self, &Keys.actionKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
get {
let action = objc_getAssociatedObject(self, &Keys.actionKey) as? Action
return action
}
}
@objc func handleAction(recognizer: UIGestureRecognizer) {
block?(recognizer)
}
convenience public init(block: @escaping ((UIGestureRecognizer) -> ())) {
self.init()
self.block = block
self.addTarget(self, action: #selector(handleAction(recognizer:)))
}
}