Lovely, closure-based touch input handling for iOS.
The old way:
let button = UIButton()
button.addTarget(self, action: #selector(buttonTouchUpInside), for: .touchUpInside)
/* .. somewhere else in your view or view controller */
func buttonTouchUpInside() {
print("tapped!")
}
The OnTap way:
let button = UIButton()
.on(.touchUpInside) { print("tapped!") }
You can even string together multiple input actions at once.
let button = UIButton()
.on(.touchDown) { print("touch down!!!") }
.on(.touchDownRepeat) { print("touch down repeat!!!") }
.on(.touchUpInside) { print("touch up inside!!!") }
.on(.touchUpOutside) { print("touch up outside!!!") }
More specifically, OnTap provides a full input abstraction over all subclasses of UIControl
, streamlines working with UIBarbuttonItem
's, and also makes adding UIGestureRecognizer
's a breeze. Essentially everything is now a button.
let button = UIButton()
.on(.touchUpInside) { print("touch up inside") }
.on(.touchDown) { print("touch down") }
let slider = UISlider().on(.valueChanged) { [unowned self] in
print("slider new value: \(self.slider.value)")
}
let rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("Right", comment: ""), style: .plain).onTap {
print(<
8727
/span>"right barButtonItem tapped!")
}
let someView = UIView()
.on(.tap, touches: 1) { print("tapped") }
.on(.leftSwipe, touches: 1) { print("left swipe 1 finger") }
.on(.leftSwipe, touches: 2) { print("left swipe 2 fingers") }
.on(.rightSwipe, touches: 1) { print("right swipe") }
.on(.upSwipe, touches: 1) { print("up swipe") }
.on(.downSwipe, touches: 1) { print("down swipe") }
OnTap is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "OnTap"
- Swift 3.0
- iOS 8.0
To run the example project, clone the repo, and run pod install
from the Example directory first.
Skye Freeman, skyefreeman@icloud.com
OnTap is available under the MIT license. See the LICENSE file for more info.