Make a UIView draggable with the finger

Alejandro L. picture Alejandro L. · Feb 4, 2013 · Viewed 12.5k times · Source

I have the mainViewController but I have an small UIView inside that is activated when you tap a MKAnnotationView in the MKMapKit, so I need that UIView to be draggable in any part of the screen.

An example screenshot of my app:

enter image description here

The circle is an example of point, I suppose I can drag for any point of the "small" UIView.

I tried using UITapGestureRecognizer but it didn't worked as my code was not good enough and I could not made it draggable because it's only the tap, not tap and move.

I hope you can help me.

Answer

Vitaly S. picture Vitaly S. · Feb 4, 2013
  1. Use UIPanGestureRecognizer instead of UITapGestureRecognizer
  2. Set userInteractionEnabled = YES for your view
  3. Review this nice tutorial about gesture recognizers: Touches. There is nice example of dragging views.