View+RxGesture.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. extension Reactive where Base: View {
  20. /**
  21. Reactive wrapper for multiple view gesture recognizers.
  22. It automatically attaches the gesture recognizers to the receiver view.
  23. The value the `Observable` emits is the gesture recognizer itself.
  24. rx.anyGesture can't error and is subscribed/observed on main scheduler.
  25. - parameter factories: a `(Factory + state)` collection you want to use to create the `GestureRecognizers` to add and observe
  26. - returns: a `ControlEvent<G>` that re-emit the gesture recognizer itself
  27. */
  28. public func anyGesture(_ factories: (AnyFactory, when: GestureRecognizerState)...) -> ControlEvent<GestureRecognizer> {
  29. let observables = factories.map { gesture, state in
  30. self.gesture(gesture).when(state).asObservable() as Observable<GestureRecognizer>
  31. }
  32. let source = Observable.from(observables).merge()
  33. return ControlEvent(events: source)
  34. }
  35. /**
  36. Reactive wrapper for multiple view gesture recognizers.
  37. It automatically attaches the gesture recognizers to the receiver view.
  38. The value the `Observable` emits is the gesture recognizer itself.
  39. rx.anyGesture can't error and is subscribed/observed on main scheduler.
  40. - parameter factories: a `Factory` collection you want to use to create the `GestureRecognizers` to add and observe
  41. - returns: a `ControlEvent<G>` that re-emit the gesture recognizer itself
  42. */
  43. public func anyGesture(_ factories: AnyFactory...) -> ControlEvent<GestureRecognizer> {
  44. let observables = factories.map { factory in
  45. self.gesture(factory).asObservable() as Observable<GestureRecognizer>
  46. }
  47. let source = Observable.from(observables).merge()
  48. return ControlEvent(events: source)
  49. }
  50. /**
  51. Reactive wrapper for a single view gesture recognizer.
  52. It automatically attaches the gesture recognizer to the receiver view.
  53. The value the `Observable` emits is the gesture recognizer itself.
  54. rx.gesture can't error and is subscribed/observed on main scheduler.
  55. - parameter factory: a `Factory` you want to use to create the `GestureRecognizer` to add and observe
  56. - returns: a `ControlEvent<G>` that re-emit the gesture recognizer itself
  57. */
  58. public func gesture<G>(_ factory: Factory<G>) -> ControlEvent<G> {
  59. return self.gesture(factory.gesture)
  60. }
  61. /**
  62. Reactive wrapper for a single view gesture recognizer.
  63. It automatically attaches the gesture recognizer to the receiver view.
  64. The value the `Observable` emits is the gesture recognizer itself.
  65. rx.gesture can't error and is subscribed/observed on main scheduler.
  66. - parameter gesture: a `GestureRecognizer` you want to add and observe
  67. - returns: a `ControlEvent<G>` that re-emit the gesture recognizer itself
  68. */
  69. public func gesture<G: GestureRecognizer>(_ gesture: G) -> ControlEvent<G> {
  70. let source = Observable.deferred {
  71. [weak control = self.base] () -> Observable<G> in
  72. MainScheduler.ensureExecutingOnScheduler()
  73. guard let control = control else { return .empty() }
  74. let genericGesture = gesture as GestureRecognizer
  75. #if os(iOS)
  76. control.isUserInteractionEnabled = true
  77. #endif
  78. control.addGestureRecognizer(gesture)
  79. return genericGesture.rx.event
  80. .map { $0 as! G }
  81. .startWith(gesture)
  82. .do(onDispose: { [weak control, weak gesture] () in
  83. guard let gesture = gesture else { return }
  84. control?.removeGestureRecognizer(gesture)
  85. })
  86. .takeUntil(control.rx.deallocated)
  87. }
  88. return ControlEvent(events: source)
  89. }
  90. }