How to drag and move a UIView

QuestionsCategory: Objective CHow to drag and move a UIView
Junior Developer asked 11 months ago

I’m making an app in which I want to drag and drop a UIView. Any help?

1 Answers
Chaudhary Talha Staff answered 11 months ago

Yes you can Drag a UIView on your view controller and where you life the finger it’ll leave it there there is a great library available. Here what you need to do:

  1. Download this
  2. Insert a UIView into your ViewController
  3. Set its IBOutlet in .h file @property (strong, nonatomic) IBOutlet UIView *loginView;
  4. Install the library using pods or manually just drop the BFDragGestureRecognizer.{h,m} files into your project.
  5. Import 
    #import “BFDragGestureRecognizer.h”
  6. Add this to your viewDidLoad Method BFDragGestureRecognizer *gesture = [[BFDragGestureRecognizer alloc] initWithTarget:self action:@selector(dragRecognized:)];
        [self.loginVIew addGestureRecognizer:gesture];
  7. Then just implement this method:
- (void)dragRecognized:(BFDragGestureRecognizer *)recognizer
    self.loginVIew = recognizer.view;
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        // When the gesture starts, remember the current position, and animate the it.
        startLocation =;
        //[self.customCam bringSubviewToFront:self.loginVIew];
        [UIView animateWithDuration:0.1 animations:^{
            self.loginVIew.transform = CGAffineTransformMakeScale(1.2, 1.2);
            self.loginVIew.alpha = 0.7;
    } else if (recognizer.state == UIGestureRecognizerStateChanged) {
        // During the gesture, we just add the gesture's translation to the saved original position.
        // The translation will account for the changes in contentOffset caused by auto-scrolling.
        CGPoint translation = [recognizer translationInView:self.loginVIew];
        CGPoint center = CGPointMake(startLocation.x + translation.x, startLocation.y + translation.y); = center;
    } else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
        [UIView animateWithDuration:0.1 animations:^{
            self.loginVIew.transform = CGAffineTransformIdentity;
            self.loginVIew.alpha = 1.0;
    } else if (recognizer.state == UIGestureRecognizerStateFailed) {