TouchDownGestureRecognizer.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #if canImport(UIKit)
  2. import UIKit.UIGestureRecognizerSubclass
  3. import RxSwift
  4. import RxCocoa
  5. public class TouchDownGestureRecognizer: UIGestureRecognizer {
  6. public override init(target: Any?, action: Selector?) {
  7. super.init(target: target, action: action)
  8. trigger
  9. .flatMapFirst { [unowned self] _ -> Observable<Void> in
  10. let trigger = Observable.just(())
  11. guard self.state == .possible else {
  12. return trigger
  13. }
  14. return trigger.delay(
  15. .milliseconds(Int(self.minimumTouchDuration * 1000)),
  16. scheduler: MainScheduler.instance
  17. )
  18. }
  19. .subscribe(onNext: { [unowned self] _ in
  20. self.touches = self._touches
  21. })
  22. .disposed(by: triggerDisposeBag)
  23. }
  24. public var minimumTouchDuration: TimeInterval = 0
  25. /**
  26. When set to `false`, it allows to bypass the touch ignoring mechanism in order to get absolutely all touch down events.
  27. Defaults to `true`.
  28. - note: See [ignore(_ touch: UITouch, for event: UIEvent)](https://developer.apple.com/documentation/uikit/uigesturerecognizer/1620010-ignore)
  29. */
  30. public var isTouchIgnoringEnabled: Bool = true
  31. @nonobjc public var touches: Set<UITouch> = [] {
  32. didSet {
  33. if touches.isEmpty {
  34. if state == .possible {
  35. state = .cancelled
  36. } else {
  37. state = .ended
  38. }
  39. } else {
  40. if state == .possible {
  41. state = .began
  42. } else {
  43. state = .changed
  44. }
  45. }
  46. }
  47. }
  48. public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
  49. super.touchesBegan(touches, with: event)
  50. setTouches(from: event)
  51. }
  52. public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
  53. super.touchesMoved(touches, with: event)
  54. setTouches(from: event)
  55. }
  56. public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
  57. super.touchesEnded(touches, with: event)
  58. setTouches(from: event)
  59. }
  60. public override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
  61. super.touchesCancelled(touches, with: event)
  62. setTouches(from: event)
  63. }
  64. private let triggerDisposeBag = DisposeBag()
  65. private let trigger = PublishSubject<Void>()
  66. private var _touches: Set<UITouch> = []
  67. private func setTouches(from event: UIEvent) {
  68. _touches = (event.allTouches ?? []).filter { touch in
  69. [.began, .stationary, .moved].contains(touch.phase)
  70. }
  71. trigger.onNext(())
  72. }
  73. public override func reset() {
  74. super.reset()
  75. touches = []
  76. }
  77. public override func ignore(_ touch: UITouch, for event: UIEvent) {
  78. guard isTouchIgnoringEnabled else {
  79. return
  80. }
  81. super.ignore(touch, for: event)
  82. }
  83. }
  84. public typealias TouchDownConfiguration = Configuration<TouchDownGestureRecognizer>
  85. public typealias TouchDownControlEvent = ControlEvent<TouchDownGestureRecognizer>
  86. public typealias TouchDownObservable = Observable<TouchDownGestureRecognizer>
  87. extension Factory where Gesture == RxGestureRecognizer {
  88. /**
  89. Returns an `AnyFactory` for `TouchDownGestureRecognizer`
  90. - parameter configuration: A closure that allows to fully configure the gesture recognizer
  91. */
  92. public static func touchDown(configuration: TouchDownConfiguration? = nil) -> AnyFactory {
  93. make(configuration: configuration).abstracted()
  94. }
  95. }
  96. extension Reactive where Base: RxGestureView {
  97. /**
  98. Returns an observable `TouchDownGestureRecognizer` events sequence
  99. - parameter configuration: A closure that allows to fully configure the gesture recognizer
  100. */
  101. public func touchDownGesture(configuration: TouchDownConfiguration? = nil) -> TouchDownControlEvent {
  102. gesture(make(configuration: configuration))
  103. }
  104. }
  105. extension ObservableType where Element: TouchDownGestureRecognizer {
  106. /**
  107. Maps the observable `GestureRecognizer` events sequence to an observable sequence of force values.
  108. */
  109. public func asTouches() -> Observable<Set<UITouch>> {
  110. self.map { $0.touches }
  111. }
  112. }
  113. #endif