Search code examples
iosrx-swiftrx-cocoa

Merge Observables of UIBarButtonItem tap and UIView tapGesture()


I have two sources for the same action. A regular RxCocoa tap in a UIBarButtonItem:

browseButton.rx.tap

and a RxGesture tap gesture recognizer in a UIView:

notConnectedView.rx.tapGesture().when(.recognized)

I subscribe to it the usual way:

<any_of_the_observables>
    .subscribe(onNext: {
        // Do things
    }).disposed(by: disposeBag)

This works with both observables, and now I want to merge it but cannot achieve it. As they are two observables of different events merge does not work.

Observable.of(notConnectedView.rx.tapGesture().when(.recognized), browseButton.rx.tap)

Type 'inout UIView' does not conform to protocol 'ReactiveCompatible'

Is it possible to make this? Thanks.


Solution

  • You should get same events type, for example convert both events to void:

    let buttonItemTap = item.rx.tap.map { _ in return () }
    let viewTap: Observable<Void> = v.rx.tapGesture().when(.recognized).map { _ in return () }
    
    Observable.of(buttonItemTap, viewTap).merge()
        .subscribe(onNext: { (_) in
            print("tapped")
        }).disposed(by: disposeBag)