diff --git a/Sources/DeclarativeLayoutKit/Chaining/Actions/UIGestureRecognizer+Action.swift b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIGestureRecognizer+Action.swift new file mode 100644 index 0000000..a390775 --- /dev/null +++ b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIGestureRecognizer+Action.swift @@ -0,0 +1,26 @@ +// +// UIGestureRecognizer+Action.swift +// +// +// Created by uuttff8 on 13.07.2021. +// + +import UIKit + + +/// **⚠️ Don't forget about ARC when use self or some parent view in action closure, to prevent retain cycle** +/// +/// Using: +/// +/// UIView() +/// .addGesture(UITapGestureRecognizer() +/// .addAction { +/// // ... +/// }) +extension UIGestureRecognizer { + func addAction(_ action: @escaping () -> Void) -> Self { + let action = ClosureAction(attachTo: self, closure: action) + self.addTarget(action, action: ClosureAction.selector) + return self + } +} diff --git a/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift index da23104..cbff40c 100644 --- a/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift +++ b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift @@ -12,6 +12,7 @@ public extension UIView { /// **⚠️ Don't forget about ARC when use self or some parent view in action closure, to prevent retain cycle** @discardableResult func onTapGesture(overwrite: Bool = false, _ action: @escaping () -> ()) -> Self { + isUserInteractionEnabled = true if overwrite { gestureRecognizers?.removeAll(where: { (gesture: UIGestureRecognizer) in gesture is UITapGestureRecognizer }) } @@ -24,6 +25,7 @@ public extension UIView { /// **⚠️ Don't forget about ARC when use self or some parent view in action closure, to prevent retain cycle** @discardableResult func onLongTapGesture(overwrite: Bool = false, _ action: @escaping () -> ()) -> Self { + isUserInteractionEnabled = true if overwrite { gestureRecognizers?.removeAll(where: { (gesture: UIGestureRecognizer) in gesture is UILongPressGestureRecognizer }) } @@ -32,4 +34,17 @@ public extension UIView { addGestureRecognizer(UILongPressGestureRecognizer(target: action, action: ClosureAction.selector)) return self } + + /// - Parameters: + /// - overwrite: if true - remove previous targets for current event. + @discardableResult + func addGesture(overwrite: Bool = false, _ gesture: Gesture) -> Self { + isUserInteractionEnabled = true + if overwrite { + gestureRecognizers?.removeAll(where: { (gesture: UIGestureRecognizer) in gesture is Gesture }) + } + + addGestureRecognizer(gesture) + return self + } }