Created
January 17, 2013 18:49
-
-
Save colinta/4558498 to your computer and use it in GitHub Desktop.
Only responds to horizontal gestures, within 4 pixels of the starting location.
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
class HorizontalPanGestureRecognizer < UIPanGestureRecognizer | |
DirectionPanThreshold = 4 | |
def initWithTarget(target, action: action) | |
super.tap do | |
my_reset | |
end | |
end | |
def touchesMoved(touches, withEvent:event) | |
super | |
return if self.state == UIGestureRecognizerStateFailed | |
nowPoint = touches.anyObject.locationInView(self.view) | |
prevPoint = touches.anyObject.previousLocationInView(self.view) | |
@move_x += prevPoint.x - nowPoint.x | |
@move_y += prevPoint.y - nowPoint.y | |
if ! @dragging | |
if @move_x.abs > DirectionPanThreshold | |
@dragging = true | |
elsif @move_y.abs > DirectionPanThreshold | |
self.state = UIGestureRecognizerStateFailed | |
@dragging = true | |
end | |
end | |
end | |
def reset | |
super | |
my_reset | |
end | |
private | |
def my_reset | |
@move_x = 0 | |
@move_y = 0 | |
@dragging = false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment