Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to drag and drop UICollectionView cell item to another UIView in iOS?

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 2.23k
    Comment on it

     

    Hi Reader’s,


    This blog includes the concept of how to drag and drop UICollectionView cell item to another UIView with the help of long press gesture. First of all given below is the snapshot of storyboard in which collection view is taken and cell includes an image view.

     

     

     

     

    Subclass of collection view cell is used to build outlet of image view. Now given below is the entire code to drag and drop UICollectionView cell item to another UIView:-

     

     

    
    
    
    SubCollectionViewCell.h
    
    
    #import <UIKit/UIKit.h>
    
    @interface SubCollectionViewCell : UICollectionViewCell
    @property (weak, nonatomic) IBOutlet UIImageView *imgView;
    
    @end
    
    
    SubCollectionViewCell.m
    
    
    #import "SubCollectionViewCell.h"
    
    @implementation SubCollectionViewCell
    
    @end
    
    
    ViewController.h
    
    
    #import <UIKit/UIKit.h>
    #import "SubCollectionViewCell.h"
    @interface ViewController : UIViewController
    
    
    @end
    
    
    ViewController.m
    
    
    #import "ViewController.h"
    
    @interface ViewController ()
    {
        NSArray *imageArray;
        CGPoint locationPoint;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
            imageArray=[[NSMutableArray alloc]initWithObjects:@"Cat.jpg",@"Dog.jpg",@"Elephant.jpg",@"Lion.jpg",@"Bear.jpg",@"Catfish.jpg",@"Jellyfish.jpg",@"Starfish.jpg",@"Bus.jpg",@"Bike.jpg",@"Tank.jpg",@"Truck.jpg",@"Arhar.jpg",@"black_mapte.jpg",@"Rajma.jpg",@"Chane.jpg",@"Frog.jpg",@"Snake.jpg",@"Sparrow.jpg",@"Parrot.jpg",@"Peigeon.jpg", nil];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return imageArray.count;
    }
    
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        
       SubCollectionViewCell *cell = [[SubCollectionViewCell alloc] init];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        [cell.imgView setImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]]];
    
        UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                              initWithTarget:self action:@selector(handleLongPress:)];
        lpgr.minimumPressDuration = 1.0; //seconds
        [cell addGestureRecognizer:lpgr];
        
        return cell;
        
    }
    
    -(void)handleLongPress:(UILongPressGestureRecognizer *)longPress
    {
       
        if(longPress.state == UIGestureRecognizerStateBegan)
        {
            
            locationPoint = [longPress locationInView:longPress.view];
            [self.view bringSubviewToFront:longPress.view];
    
        }
        else if (longPress.state==UIGestureRecognizerStateEnded)
        {
       
            [self.view bringSubviewToFront:longPress.view];
    
        }
    
        CGPoint newCoord = [longPress locationInView:longPress.view];
        float dX = newCoord.x-locationPoint.x;
        float dY = newCoord.y-locationPoint.y;
        longPress.view.frame = CGRectMake(longPress.view.frame.origin.x+dX,
                                          longPress.view.frame.origin.y+dY,
                                          longPress.view.frame.size.width,
                                          longPress.view.frame.size.height);
        [self.view addSubview:longPress.view];
    }
    
    @end
    

     

    I hope this blog will help you to understand the concept of drag and drop UICollectionView cell item to another UIView. You can also download the sample project from the link given below:-

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: