Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // 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())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement