From 4093c65653d4df55ad74e10f8989aebe4bea7ecb Mon Sep 17 00:00:00 2001 From: Anton Kuzmin Date: Tue, 13 Jul 2021 19:28:00 +0300 Subject: [PATCH 1/3] add isUserInteractionEnabled to uivew gestures --- .../DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift index da23104..c9bb10f 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 }) } From a82f9b930fed72627ca365f4bfbca80ed4e61206 Mon Sep 17 00:00:00 2001 From: Anton Kuzmin Date: Tue, 13 Jul 2021 19:34:52 +0300 Subject: [PATCH 2/3] simplify using uigestureRecognizer + uiview additions --- .../Actions/UIGestureRecognizer+Action.swift | 26 +++++++++++++++++++ .../Chaining/Actions/UIView+Gesture.swift | 5 ++++ 2 files changed, 31 insertions(+) create mode 100644 Sources/DeclarativeLayoutKit/Chaining/Actions/UIGestureRecognizer+Action.swift diff --git a/Sources/DeclarativeLayoutKit/Chaining/Actions/UIGestureRecognizer+Action.swift b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIGestureRecognizer+Action.swift new file mode 100644 index 0000000..b83d624 --- /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 + + +/// Add action to a UIGestureRecognizer. +/// +/// Using: +/// +/// UIView() +/// .addGesture(gesture: 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 c9bb10f..6b19cb1 100644 --- a/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift +++ b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift @@ -34,4 +34,9 @@ public extension UIView { addGestureRecognizer(UILongPressGestureRecognizer(target: action, action: ClosureAction.selector)) return self } + + func addGesture(overwrite: Bool = false, gesture: Gesture) -> Self { + addGestureRecognizer(gesture) + return self + } } From 4cdea6c38e9da57597373ce7c3d396a3e5b8453e Mon Sep 17 00:00:00 2001 From: Anton Kuzmin Date: Tue, 13 Jul 2021 22:14:37 +0300 Subject: [PATCH 3/3] Adding Doc comments --- .../Chaining/Actions/UIGestureRecognizer+Action.swift | 10 +++++----- .../Chaining/Actions/UIView+Gesture.swift | 10 +++++++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Sources/DeclarativeLayoutKit/Chaining/Actions/UIGestureRecognizer+Action.swift b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIGestureRecognizer+Action.swift index b83d624..a390775 100644 --- a/Sources/DeclarativeLayoutKit/Chaining/Actions/UIGestureRecognizer+Action.swift +++ b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIGestureRecognizer+Action.swift @@ -8,15 +8,15 @@ import UIKit -/// Add action to a UIGestureRecognizer. +/// **⚠️ Don't forget about ARC when use self or some parent view in action closure, to prevent retain cycle** /// /// Using: /// /// UIView() -/// .addGesture(gesture: UITapGestureRecognizer() -/// .addAction { -/// // ... -/// }) +/// .addGesture(UITapGestureRecognizer() +/// .addAction { +/// // ... +/// }) extension UIGestureRecognizer { func addAction(_ action: @escaping () -> Void) -> Self { let action = ClosureAction(attachTo: self, closure: action) diff --git a/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift index 6b19cb1..cbff40c 100644 --- a/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift +++ b/Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift @@ -35,7 +35,15 @@ public extension UIView { return self } - func addGesture(overwrite: Bool = false, gesture: Gesture) -> 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 }