Adds closure support to UIControls and UIViews with UIGestureRecognizers.
- iOS 9.0+
- Xcode 10.2+
- Swift 5.0+
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate the library into your Xcode project using CocoaPods, specify it in your Podfile
:
pod 'UIHandlers', '1.6.1'
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
dependencies: [
.package(url: "https://github.com/berbschloe/UIHandlers.git", from: "1.6.1")
]
It would be recommended to add the library globally because it can get annoying importing it everywhere.
// Add this to a GlobalImports.swift
@_exported import UIHandlers
func viewDidLoad() {
super.viewDidLoad()
let button = UIButton()
button.addTarget(self, action: #selector(doSomething), for: .primaryActionTriggered)
}
@objc
private func doSomething() { ... }
func viewDidLoad() {
super.viewDidLoad()
let button = UIButton()
// [unowned self] is needed to prevent reference cycles
button.addHandler(for: .primaryActionTriggered) { [unowned self] in
self.doSomething();
}
}
button.addHandler(for: .primaryActionTriggered) { ... }
button.addHandler(for: .primaryActionTriggered) ( (sender: UIButton) in ... }
button.addHandler(for: .primaryActionTriggered) ( (sender: UIButton, event: UIEvent) in ... }
func viewDidLoad() {
super.viewDidLoad()
let view = UIView()
let gesture = UITapGestureRecognizer(target: self, action: #selector(doSomething))
view.addGestureRecognizer(gesture)
}
@objc
private func doSomething() { ... }
func viewDidLoad() {
super.viewDidLoad()
let view = UIView()
view.addTapHandler { [unowned self] in
self.doSomething()
}
}
// tap handler with paramater
view.addTapHandler { (gesture: UITapGestureRecognizer) in ... }
// controling the number of taps
view.addTapHandler(numberOfTapsRequired: 42) { ... }
view.addDoubleTapHandler { ... }
view.addLongPressHandler { ... }
view.addPanHandler { ... }
view.addSwipeHandler(direction: .right) { ... }
view.addPinchHandler { ... }
view.addRotationHandler { ... }
view.addScreenEdgePanHandler(edges: .right) { ... }