Created
August 24, 2017 08:34
-
-
Save wanbok/12d09b7ce780de38d9a96dc4a5d1703d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CancelableTapGestureRecognizer.swift | |
// | |
// Created by Wanbok Choi on 2017. 5. 4.. | |
// | |
import UIKit.UIGestureRecognizerSubclass | |
import RxSwift | |
import RxCocoa | |
import RxGesture | |
final class SensitiveTapGestureRecognizer: UIGestureRecognizer { | |
let delta: CGFloat = 4 | |
var position: CGPoint? | |
func distance(a: CGPoint, b: CGPoint) -> CGFloat { | |
let xDist = a.x - b.x | |
let yDist = a.y - b.y | |
return CGFloat(sqrt((xDist * xDist) + (yDist * yDist))) | |
} | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { | |
self.position = touches.first?.location(in: self.view) | |
} | |
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) { | |
guard let currentPosition = touches.first?.location(in: self.view), | |
let beginingPosition = self.position, | |
currentPosition ~ beginingPosition < self.delta | |
else { | |
self.state = .failed | |
return | |
} | |
} | |
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) { | |
defer { self.position = nil } | |
self.state = .cancelled | |
} | |
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) { | |
defer { self.position = nil } | |
guard self.state == .possible else { return } | |
self.state = .recognized | |
} | |
} | |
struct SensitiveTapGestureRecognizerFactory: GestureRecognizerFactory { | |
typealias Gesture = SensitiveTapGestureRecognizer | |
let configuration: (SensitiveTapGestureRecognizer) -> Void | |
/** | |
Initialiaze a `GestureRecognizerFactory` for `UITapGestureRecognizer` | |
- parameter numberOfTouchesRequired: The number of fingers required to match | |
- parameter numberOfTapsRequired: The number of taps required to match | |
- parameter configuration: A closure that allows to fully configure the gesture recognizer | |
*/ | |
init(){ | |
self.configuration = { gesture in } | |
} | |
} | |
extension Reactive where Base: UIView { | |
var sensitiveTapGesture: ControlEvent<SensitiveTapGestureRecognizer> { | |
return gesture(SensitiveTapGestureRecognizerFactory()) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment