Search code examples
objective-cipaddrag-and-dropuiimageview

drag and drop uiimage into another uiimageview


I am using following code snippet to drag and drop uiimageview

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[myImageView addGestureRecognizer:panRecognizer];

-(void)move:(id)sender {

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

        firstX = [myImageView  center].x;
        firstY = [myImageView  center].y;
    }

    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
    [myImageView  setCenter:translatedPoint];

}

This code is drags the whole myImageView ,but my requirement is to just drag the uiimage and drop it into another uiimagview.myImageView should stay as it is after dragging also.just I need to drag the myImageView layer.draggable image should be transparent. Any ideas would b appreciated.


Solution

  • I have put little effort to achieve your output. try it

    Step 1 :Define this 3 Variables in your .h file

    UIImageView *ivSource1, *ivDestination2, *tempIV;
    

    Step 2 : Initialize all the three UIImageView and add to your ViewController write it in viewDidLoad method

    ivSource1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
    [ivSource1 setFrame:CGRectMake(100, 100, 100, 100)];
    [ivSource1 setTag:100];
    [ivSource1 setUserInteractionEnabled:YES];    
    [self.view addSubview:ivSource1];
    
    ivDestination2 = [[UIImageView alloc] init];
    [ivDestination2 setFrame:CGRectMake(200, 300, 100, 100)];
    [ivDestination2 setTag:101];
    [ivDestination2 setUserInteractionEnabled:YES];
    [self.view addSubview:ivDestination2];
    
    tempIV = [[UIImageView alloc] init];
    [tempIV setFrame:CGRectMake(0, 300, 100, 100)];
    [tempIV setTag:102];
    [tempIV setUserInteractionEnabled:YES];
    [self.view addSubview:tempIV];
    

    Step 3 : Define following touch methods to handle movement of image for Drag & Drop

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
    
        if([[touch view] tag] == 100)
        {
            [tempIV setImage:ivSource1.image];
            [tempIV setCenter:[touch locationInView:self.view]];
        }
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
    
        [tempIV setCenter:[touch locationInView:self.view]];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        [tempIV setCenter:[touch locationInView:self.view]];
    
        if(CGRectContainsPoint(ivDestination2.frame, [touch locationInView:self.view]))
        {
            [ivDestination2 setImage:tempIV.image];
        }
        // Remove image from dragable view
        [tempIV setImage:[UIImage imageNamed:@""]];    
    }