Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a UIView draggable with the finger

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.

like image 525
Alejandro L. Avatar asked Feb 04 '13 09:02

Alejandro L.


3 Answers

edited after a commend by @borrrden

UIPanGestureRecognizer is suitable. In your handler function check it's state variable.

typedef enum {
   UIGestureRecognizerStatePossible,

   UIGestureRecognizerStateBegan,     // this will be the value on touch
   UIGestureRecognizerStateChanged,   // ~ on drag
   UIGestureRecognizerStateEnded,     // ~ on end of touch event
   UIGestureRecognizerStateCancelled,

   UIGestureRecognizerStateFailed,

   UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
} UIGestureRecognizerState;
like image 35
Alexander Avatar answered Sep 28 '22 07:09

Alexander


  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.
like image 50
Vitaly S. Avatar answered Sep 28 '22 07:09

Vitaly S.


For create draggable and resizable UIView This example (with source code) really useful for you.

And also read This Document and This Document This document is related to UIPanGestureRecognizer

like image 35
iPatel Avatar answered Sep 28 '22 08:09

iPatel