Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
}
15 changes: 15 additions & 0 deletions Sources/DeclarativeLayoutKit/Chaining/Actions/UIView+Gesture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand All @@ -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 })
}
Expand All @@ -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<Gesture: UIGestureRecognizer>(overwrite: Bool = false, _ gesture: Gesture) -> Self {
isUserInteractionEnabled = true
if overwrite {
gestureRecognizers?.removeAll(where: { (gesture: UIGestureRecognizer) in gesture is Gesture })
}

addGestureRecognizer(gesture)
return self
}
}