Drag & Drop iOS

In iOS, we can detect drag and drop(swipe operation).
This is a sample for it.

Sample

This sample is to create new view in ViewController and set pan event
using UIPanGestureRecognizer

ViewController.m

@interface ViewController ()
@property (nonatomic) UIView *dragView;
@end

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self.view setBackgroundColor:[UIColor blueColor]];
    
    // Test Drag
    [self dragViewTest];
}

#pragma mark -
#pragma mark - Drag and Drop
-(void)dragViewTest {
    // Create View
    CGRect rect = CGRectMake(10, 10, 300, 100);
    self.dragView = [[UIView alloc] initWithFrame:rect];
    [self.dragView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:self.dragView];
    
    // Drag
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragView:)];
    // Add Gesture to view
    [self.dragView addGestureRecognizer:pan];
}

-(void)dragView:(UIPanGestureRecognizer *)sender {
    CGPoint location = [sender translationInView:self.view];
    NSLog(@"Drag x %f, y %f", location.x, location.y);
    
    self.dragView.frame = CGRectMake(location.x, 10, 300, 100);
}

The important thing is to create UIPanGestureRecognizer instance and set it to the target view. In this case, it is view which is created in dragViewTest method.

Result

drag and drop iOS