Skip to content

Instantly share code, notes, and snippets.

@colinta
Created January 17, 2013 18:49
Show Gist options
  • Save colinta/4558498 to your computer and use it in GitHub Desktop.
Save colinta/4558498 to your computer and use it in GitHub Desktop.
Only responds to horizontal gestures, within 4 pixels of the starting location.
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