A safer and more idiomatic way to respond to gestures and control events
Tactile is a safer and more idiomatic way to respond to gestures and control events. It lets you catch bugs at compile time and write more expressive code.
view.pan([
.began: panBegan,
.changed: panChanged,
.ended: panEnded
])
// func panBegan(pan: UIPanGestureRecognizer)
// func panChanged(pan: UIPanGestureRecognizer)
// func panEnded(pan: UIPanGestureRecognizer)
Usage • Installation • License
Usage
Tactile extends both UIView
and UIControl
classes.
UIView extensions
The on
method
Use the on
method to add gesture recognizers.
on(gesture:callback:)
let tap = UITapGestureRecognizer()
tap.numberOfTapsRequired = 3
tap.numberOfTouchesRequired = 2
view.on(tap, tapped)
// func tapped(tap: UITapGestureRecognizer)
on(gesture:state:callback:)
let pinch = UIPinchGestureRecognizer()
view.on(pinch, .began, pinchBegan)
// func pinchBegan(pinch: UIPinchGestureRecognizer)
on(gesture:states:callback:)
let pan = UIPanGestureRecognizer()
view.on(pan, [.began, .ended], panBeganOrEnded)
// func panBeganOrEnded(pan: UIPanGestureRecognizer)
on(gesture:callbacks:)
let pinch = UIPinchGestureRecognizer()
view.on(pinch, [
.began: pinchBegan,
.ended: pinchEnded
])
// func pinchBegan(pinch: UIPinchGestureRecognizer)
// func pinchEnded(pinch: UIPinchGestureRecognizer)
The shorthand methods
Tactile defines 6 shorthand methods: longPress
, pan
, pinch
, rotation
, swipe
and tap
.
<shorthand>(callback:)
view.tap(tapped)
// func tapped(tap: UITapGestureRecognizer)
<shorthand>(state:callback:)
view.pinch(.began, pinchBegan)
// func pinchBegan(pinch: UIPinchGestureRecognizer)
<shorthand>(states:callback:)
view.pan([.began, .ended], panBeganOrEnded)
// func panBeganOrEnded(pan: UIPanGestureRecognizer)
<shorthand>(callbacks:)
view.longPress([
.began: longPressBegan,
.ended: longPressEnded
])
// func longPressBegan(longPress: UILongPressGestureRecognizer)
// func longPressEnded(longPress: UILongPressGestureRecognizer)
The off
method
Use the off
method to remove gesture recognizers.
off(gesture:)
let tap = UITapGestureRecognizer()
view.on(tap, tapped)
// ...
view.off(tap)
off(gestureType:)
view.off(UITapGestureRecognizer.self)
off()
view.off()
Attaching a gesture recognizer to multiple views
With Tactile, you can attach the same gesture recognizer to multiple views.
let tap = UITapGestureRecognizer()
tap.numberOfTapsRequired = 3
tap.numberOfTouchesRequired = 2
firstView.on(tap, firstViewTapped)
secondView.on(tap, secondViewTapped)
UIControl extensions
Use the on
method to attach an event handler function for one or more control events.
on(event:callback:)
button.on(.touchUpInside, tapped)
// func tapped(button: UIButton)
on(events:callback:)
button.on([.touchUpInside, .touchUpOutside], tapped)
// func tapped(button: UIButton)
on(callbacks:)
button.on([
.touchUpInside: tapped,
.touchUpOutside: cancelledTap
])
// func tapped(button: UIButton)
// func cancelledTap(button: UIButton)
Installation
Carthage
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Tactile into your Xcode project using Carthage, specify it in your Cartfile
:
github "delba/Tactile" >= 1.0
CocoaPods
CocoaPods is a dependency manager for Cocoa projects.
You can install it with the following command:
$ gem install cocoapods
To integrate Tactile into your Xcode project using CocoaPods, specify it in your Podfile
:
use_frameworks!
pod 'Tactile', '~> 1.0'
License
Copyright (c) 2015-2019 Damien