1

I am using a UIPanGestureRecogniser to implement drag and drop. When the drag starts I need to identify the object that is being dragged. However the objects are relatively small. And if the user doesn't hit the object right in the centre of the object it isn't getting dragged.

The problem is that when the gesture handler is first called with the state UIGestureRecognizerStateBegan, the finger has already moved several pixels, and so [UIPanGestureRecognizer locationInView:] returns that point, which is not where the gesture truly started. That makes sense as it can only recognize a pan after a few pixels of movement. However, I need the absolute start of the gesture, not the position after the gesture has first been recognized.

I'm thinking that maybe I need to implement a tap gesture recognizer as well, purely to capture the first touch. But that seems like a hack for what is not an unusual requirement. Is there no other way of getting that first touch from within the pan gesture recognizer?

Steve Waddicor
  • 2,197
  • 15
  • 20
  • Have you thought about perhaps making use of the event-handling methods `touchesBegan:` and `touchesMoved:` methods by subclassing the `UIView`? – sooper Jul 05 '14 at 16:17
  • How big are the views being dragged? Ideally, they should be *at least* 44x44 points. If they meet that requirement, then perhaps we could solve your problem. – duci9y Jul 05 '14 at 16:20
  • Right now the smallest object is 44x46 points. It's a generic problem that will affect any dragged object of any size. It's just more obvious with smaller objects as the area in the middle of the object that is always draggable is relatively smaller. – Steve Waddicor Jul 05 '14 at 19:03
  • Does this answer your question? [UIPanGestureRecognizer starting point is off](https://stackoverflow.com/questions/2861400/uipangesturerecognizer-starting-point-is-off) – Senseful Mar 08 '20 at 22:43

1 Answers1

1

UIGestureRecognizerDelegate protocol provides methods gestureRecognizerShouldBegin: and gestureRecognizer:shouldReceiveTouch: that can help you evaluate the touches before the pan has transitioned to state UIPanGestureRecognizerStateBegan

spassas
  • 4,778
  • 2
  • 31
  • 39
  • Unfortunaly at `gestureRecognizer:shouldReceiveTouch:` `locationInView:` still has the location from the previous pan. And at `gestureRecognizerShouldBegin:` the location is the same as I already get in the normal handler. `locationOfTouch:inView:` is similarly unhelpful at the time of either of those methods. – Steve Waddicor Jul 05 '14 at 19:41
  • If you call `gesture.locationInView`, then yes, you'll get a bad value. But if you call `touch.locationInView`, you'll get an accurate value. Note that the callback method gives you both objects (gesture and touch). See [this answer](https://stackoverflow.com/a/60592833/35690) for more details. – Senseful Mar 08 '20 at 22:53