Skip to content

Instantly share code, notes, and snippets.

@aurozhkov
Last active December 21, 2015 05:30
Show Gist options
  • Save aurozhkov/38616655834cb00358cb to your computer and use it in GitHub Desktop.
Save aurozhkov/38616655834cb00358cb to your computer and use it in GitHub Desktop.
UIDoubleTapAndMoveGestureRecognizer
import UIKit
import UIKit.UIGestureRecognizerSubclass
class UIDoubleTapAndMoveGestureRecognizer : UIGestureRecognizer {
private var timer : NSTimer?
private var isFirstTapDetected = false
private var isDoubleTapDetected = false
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
if touches.count != 1 || isDoubleTapDetected {
isFirstTapDetected = false
state = .Failed
return
}
if !isFirstTapDetected {
isFirstTapDetected = true
timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "doubleTapTimeExpired", userInfo: nil, repeats: false)
state = .Possible
} else {
isFirstTapDetected = false
isDoubleTapDetected = true
timer?.invalidate()
state = .Began
}
}
func doubleTapTimeExpired() {
state = .Failed
isFirstTapDetected = false
isDoubleTapDetected = false
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
if !isFirstTapDetected {
state = .Ended
}
isDoubleTapDetected = false
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
if !isDoubleTapDetected {
state = .Failed
}
if state == .Failed {
isFirstTapDetected = false
isDoubleTapDetected = false
return
}
state = .Changed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment