TouchDownGestureRecognizer.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import UIKit.UIGestureRecognizerSubclass
  2. import RxSwift
  3. import RxCocoa
  4. public class TouchDownGestureRecognizer: UILongPressGestureRecognizer {
  5. public override init(target: Any?, action: Selector?) {
  6. super.init(target: target, action: action)
  7. minimumPressDuration = 0.0
  8. }
  9. /**
  10. When set to `false`, it allows to bypass the touch ignoring mechanism in order to get absolutely all touch down events.
  11. Defaults to `true`.
  12. - note: See [ignore(_ touch: UITouch, for event: UIEvent)](https://developer.apple.com/documentation/uikit/uigesturerecognizer/1620010-ignore)
  13. */
  14. public var isTouchIgnoringEnabled: Bool = true
  15. @nonobjc public var touches: Set<UITouch> = []
  16. public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
  17. super.touchesBegan(touches, with: event)
  18. self.touches.formUnion(touches)
  19. }
  20. public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
  21. super.touchesMoved(touches, with: event)
  22. self.touches.formUnion(touches)
  23. }
  24. public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
  25. super.touchesEnded(touches, with: event)
  26. self.touches.subtract(touches)
  27. }
  28. public override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
  29. super.touchesCancelled(touches, with: event)
  30. self.touches.subtract(touches)
  31. }
  32. public override func reset() {
  33. super.reset()
  34. touches = []
  35. }
  36. public override func ignore(_ touch: UITouch, for event: UIEvent) {
  37. guard isTouchIgnoringEnabled else {
  38. return
  39. }
  40. super.ignore(touch, for: event)
  41. }
  42. }
  43. public typealias TouchDownConfiguration = Configuration<TouchDownGestureRecognizer>
  44. public typealias TouchDownControlEvent = ControlEvent<TouchDownGestureRecognizer>
  45. public typealias TouchDownObservable = Observable<TouchDownGestureRecognizer>
  46. extension Factory where Gesture == GestureRecognizer {
  47. /**
  48. Returns an `AnyFactory` for `TouchDownGestureRecognizer`
  49. - parameter configuration: A closure that allows to fully configure the gesture recognizer
  50. */
  51. public static func touchDown(configuration: TouchDownConfiguration? = nil) -> AnyFactory {
  52. return make(configuration: configuration).abstracted()
  53. }
  54. }
  55. extension Reactive where Base: View {
  56. /**
  57. Returns an observable `TouchDownGestureRecognizer` events sequence
  58. - parameter configuration: A closure that allows to fully configure the gesture recognizer
  59. */
  60. public func touchDownGesture(configuration: TouchDownConfiguration? = nil) -> TouchDownControlEvent {
  61. return gesture(make(configuration: configuration))
  62. }
  63. }
  64. extension ObservableType where Element: TouchDownGestureRecognizer {
  65. /**
  66. Maps the observable `GestureRecognizer` events sequence to an observable sequence of force values.
  67. */
  68. public func asTouches() -> Observable<Set<UITouch>> {
  69. return self.map { $0.touches }
  70. }
  71. }