Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. //
  2. // CancelableTapGestureRecognizer.swift
  3. //
  4. // Created by Wanbok Choi on 2017. 5. 4..
  5. //
  6.  
  7. import UIKit.UIGestureRecognizerSubclass
  8.  
  9. import RxSwift
  10. import RxCocoa
  11. import RxGesture
  12.  
  13. final class SensitiveTapGestureRecognizer: UIGestureRecognizer {
  14. let delta: CGFloat = 4
  15. var position: CGPoint?
  16.  
  17. func distance(a: CGPoint, b: CGPoint) -> CGFloat {
  18. let xDist = a.x - b.x
  19. let yDist = a.y - b.y
  20. return CGFloat(sqrt((xDist * xDist) + (yDist * yDist)))
  21. }
  22.  
  23. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
  24. self.position = touches.first?.location(in: self.view)
  25. }
  26. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
  27. guard let currentPosition = touches.first?.location(in: self.view),
  28. let beginingPosition = self.position,
  29. currentPosition ~ beginingPosition < self.delta
  30. else {
  31. self.state = .failed
  32. return
  33. }
  34. }
  35. override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
  36. defer { self.position = nil }
  37. self.state = .cancelled
  38. }
  39. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
  40. defer { self.position = nil }
  41. guard self.state == .possible else { return }
  42. self.state = .recognized
  43. }
  44. }
  45.  
  46. struct SensitiveTapGestureRecognizerFactory: GestureRecognizerFactory {
  47. typealias Gesture = SensitiveTapGestureRecognizer
  48. let configuration: (SensitiveTapGestureRecognizer) -> Void
  49.  
  50. /**
  51. Initialiaze a `GestureRecognizerFactory` for `UITapGestureRecognizer`
  52. - parameter numberOfTouchesRequired: The number of fingers required to match
  53. - parameter numberOfTapsRequired: The number of taps required to match
  54. - parameter configuration: A closure that allows to fully configure the gesture recognizer
  55. */
  56. init(){
  57. self.configuration = { gesture in }
  58. }
  59. }
  60.  
  61. extension Reactive where Base: UIView {
  62.  
  63. var sensitiveTapGesture: ControlEvent<SensitiveTapGestureRecognizer> {
  64. return gesture(SensitiveTapGestureRecognizerFactory())
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement