You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.9 KiB

  1. // Copyright (c) RxSwiftCommunity
  2. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3. // of this software and associated documentation files (the "Software"), to deal
  4. // in the Software without restriction, including without limitation the rights
  5. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. // copies of the Software, and to permit persons to whom the Software is
  7. // furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included in
  9. // all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  16. // THE SOFTWARE.
  17. import RxSwift
  18. import RxCocoa
  19. import Dispatch
  20. extension Reactive where Base: RxGestureView {
  21. /**
  22. Reactive wrapper for multiple view gesture recognizers.
  23. It automatically attaches the gesture recognizers to the receiver view.
  24. The value the `Observable` emits is the gesture recognizer itself.
  25. rx.anyGesture can't error and is subscribed/observed on main scheduler.
  26. - parameter factories: a `(Factory + state)` collection you want to use to create the `GestureRecognizers` to add and observe
  27. - returns: a `ControlEvent<G>` that re-emit the gesture recognizer itself
  28. */
  29. public func anyGesture(_ factories: (AnyFactory, when: RxGestureRecognizerState)...) -> ControlEvent<RxGestureRecognizer> {
  30. let observables = factories.map { gesture, state in
  31. self.gesture(gesture).when(state).asObservable() as Observable<RxGestureRecognizer>
  32. }
  33. let source = Observable.from(observables).merge()
  34. return ControlEvent(events: source)
  35. }
  36. /**
  37. Reactive wrapper for multiple view gesture recognizers.
  38. It automatically attaches the gesture recognizers to the receiver view.
  39. The value the `Observable` emits is the gesture recognizer itself.
  40. rx.anyGesture can't error and is subscribed/observed on main scheduler.
  41. - parameter factories: a `Factory` collection you want to use to create the `GestureRecognizers` to add and observe
  42. - returns: a `ControlEvent<G>` that re-emit the gesture recognizer itself
  43. */
  44. public func anyGesture(_ factories: AnyFactory...) -> ControlEvent<RxGestureRecognizer> {
  45. let observables = factories.map { factory in
  46. self.gesture(factory).asObservable() as Observable<RxGestureRecognizer>
  47. }
  48. let source = Observable.from(observables).merge()
  49. return ControlEvent(events: source)
  50. }
  51. /**
  52. Reactive wrapper for a single view gesture recognizer.
  53. It automatically attaches the gesture recognizer to the receiver view.
  54. The value the `Observable` emits is the gesture recognizer itself.
  55. rx.gesture can't error and is subscribed/observed on main scheduler.
  56. - parameter factory: a `Factory` you want to use to create the `GestureRecognizer` to add and observe
  57. - returns: a `ControlEvent<G>` that re-emit the gesture recognizer itself
  58. */
  59. public func gesture<G>(_ factory: Factory<G>) -> ControlEvent<G> {
  60. self.gesture(factory.gesture)
  61. }
  62. /**
  63. Reactive wrapper for a single view gesture recognizer.
  64. It automatically attaches the gesture recognizer to the receiver view.
  65. The value the `Observable` emits is the gesture recognizer itself.
  66. rx.gesture can't error and is subscribed/observed on main scheduler.
  67. - parameter gesture: a `GestureRecognizer` you want to add and observe
  68. - returns: a `ControlEvent<G>` that re-emit the gesture recognizer itself
  69. */
  70. public func gesture<G: RxGestureRecognizer>(_ gesture: G) -> ControlEvent<G> {
  71. let source = Observable.deferred { [weak control = self.base] () -> Observable<G> in
  72. MainScheduler.ensureExecutingOnScheduler()
  73. guard let control = control else { return .empty() }
  74. let genericGesture = gesture as RxGestureRecognizer
  75. #if os(iOS)
  76. control.isUserInteractionEnabled = true
  77. #endif
  78. control.addGestureRecognizer(gesture)
  79. return genericGesture.rx.event
  80. .compactMap { $0 as? G }
  81. .startWith(gesture)
  82. .do(onDispose: { [weak control, weak gesture] () in
  83. guard let gesture = gesture else { return }
  84. DispatchQueue.main.async {
  85. control?.removeGestureRecognizer(gesture)
  86. }
  87. })
  88. .take(until: control.rx.deallocated)
  89. }
  90. return ControlEvent(events: source)
  91. }
  92. }