To run the example project, clone the repo, in the Example folder open RxGesture.xcworkspace
.
You might need to run pod install
from the Example directory first.
RxGesture allows you to easily turn any view into a tappable or swipeable control like so:
myView.rx_gesture(.Tap).subscribeNext {_ in
//react to taps
}.addDisposableTo(stepBag)
You can also react to more than one type of gesture. For example to dismiss a photo preview you might want to do that when the user taps it, or swipes up or down:
myView.rx_gesture(.Tap, .SwipeUp, .SwipeDown).subscribeNext {_ in
//dismiss presented photo
}.addDisposableTo(stepBag)
rx_gesture
is defined as Observable<RxGestureTypeOption>
so what it emits is the concrete type of the gesture that was triggered (handy if you are observing more than one type)
On iOS RXGesture supports:
- .Tap
- .SwipeLeft, SwipeRight, SwipeUp, SwipeDown
- .LongPress
- .Pan(.Began), .Pan(.Changed), .Pan(.Ended), .Pan(.Any)
- .Rotate(.Began), .Rotate(.Changed), .Rotate(.Ended), .Rotate(.Any)
On OSX RXGesture supports:
- .Click
- .RightClick
- .Pan(.Began), .Pan(.Changed), .Pan(.Ended), .Pan(.Any) (if used in one call to
rx_gesture
untilNSGestureRecognizer
implementsrx_event
) - .Rotate(.Began), .Rotate(.Changed), .Rotate(.Ended), .Rotate(.Any) (if used in one call to
rx_gesture
untilNSGestureRecognizer
implementsrx_event
)
If you are writing multi-platform code you can eventually write:
myView.rx_gesture(.Tap, .Click).subscribeNext {...}
to observe for the concrete gesture on each platform.
Some recognizers fire a single event per gesture and don't provide any values. For example .Tap
just lets you know a view has been tapped - that's all.
Other recognizers provide details about the gesture (that also might be ongoing). For example the pan gesture will continuously provide you with the offset from the initial point where the gesture started:
myView.rx_gesture(.Pan(.Changed)).subscribeNext {[weak self] gesture in
switch gesture {
case .Pan(let data):
print("offset: \(data.translation)")
default: break
}
}.addDisposableTo(bag)
Pattern match the associated value of type PanConfig
to get the translation, velocity, and a ref to the recognizer itself.
The demo app includes examples for all recognizers.
This library depends on both RxSwift and RxCocoa.
RxGesture is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "RxGesture"
- defo add tests
- make pr to rxcocoa to add native support for rx_event to
NSGestureRecognizer
and remove the implementation from this repo
Everyone in the RxSwift Slack channel ๐ฏ
RxGesture is available under the MIT license. See the LICENSE file for more info.