(use it and modify it freely, just don't forget to thank me)
The Android SDK allows developers take any View
object and set an OnClickListener
object to it, now you can also do it in Swift!
Take any UIView
object and set a "listener" with an action inside a closure by using this CocoaPod project.
This project supplies lightweight Swift extensions that let developers use an onGesture
method, such as: onClick
, onLongPress
, onSwipe
, onPan
(and even onDrag
that will allow dragging the view).
It's very similar to the Android SDK's setOnClickListener(...)
method.
import OnGestureSwift
...
someView.onClick() { (tapGestureRecognizer: UITapGestureRecognizer) in
// Do something...
}
import OnGestureSwift
...
mapView.onLongPress({ [weak self] (longPressGestureRecognizer) in
guard longPressGestureRecognizer.state == .began, let mapView = self?.mapView else { return }
let longPressedLocationCoordinate = mapView.convert(longPressGestureRecognizer.location(in: mapView), toCoordinateFrom: mapView)
print("tapped on location's coordinate: \(longPressedLocationCoordinate)")
// Do stuff with 'longPressedLocationCoordinate'
})
import OnGestureSwift
...
override func viewDidLoad() {
super.viewDidLoad()
view.onSwipe(direction: .down) { [unowned self] _ in
self.dismiss(animated: true, completion: nil)
}
...
}
import OnGestureSwift
...
someButton.onPan { [weak self] (panGestureRecognizer) in
guard let strongSelf = self else { return }
if let superview = panGestureRecognizer.view?.superview {
let locationOfPan = panGestureRecognizer.location(in: superview)
// Do something with 'locationOfPan'
}
}
import OnGestureSwift
...
someLabel.onDrag(predicateClosure: { _ in
return true
}, onDragClosure: { dragGestureListener in
guard let draggingPoint = dragGestureListener.pannedPoint else { return }
// Do something with 'draggingPoint'
})
To install this pod simply add pod 'OnGestureSwift'
to your Podfile like this example:
target 'MyApp' do
pod 'OnGestureSwift'
end
Then run a pod install
inside your terminal, or from CocoaPods.app.
Don't worry about memory leaks, this implementation has been tested, reviewed, used and improved - it works perfectly with ARC. I did try using objc_setAssociatedObject
in the past but I saw it caused memory leaks so it's not recommended. The selected solution has no memory issues, so keep calm and free your mind. :)
Just make sure you use [weak self]
/ [unowned self]
because you don't want to keep a strong references to self
(basically, usually we would like not to hold strong references to self
inside closures).
Enjoy :)