Pages

Wednesday, January 30, 2013

Drag and Drop UIImageView(s) inside UIView

i have put a UIView inside the xib and made the IBOutlet for this as property name - sampleView.

This UIView's x and y position is as per my project. So no need to worry about the parent view x and y position. Because we can make it relative to superview controller as follow:

//imagesView is Mutable Array and contains all UIImages.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
    
    for(int i=0;i<imagesInView.count;i++)
    {
        UIImageView *imgView = [imagesInView objectAtIndex:i];
        if (CGRectContainsPoint([imgView frame], [touch locationInView:self.secondFullImageView])) {
            //if ([touch tapCount] == 2)
                //[imgView setupNextDisplayString];
            CGPoint touchPoint = [touch locationInView:self.sampleView];//Here you need to concentrate for your UIImageView Parent
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
            imgView.center = touchPoint;
            CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
            imgView.transform = transform;
            [UIView commitAnimations];
            return;
        }
   }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    for(int i=0;i<imagesInView.count;i++)
    {
        UIImageView *imgView = [imagesInView objectAtIndex:i];
        if (CGRectContainsPoint([imgView frame], [touch locationInView:self.sampleView])) {
            CGPoint location = [touch locationInView:self.sampleView];
            imgView.center = location;
        }
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    
    for(int i=0;i<imagesInView.count;i++)
    {
        UIImageView *imgView = [imagesInView objectAtIndex:i];
        if (CGRectContainsPoint([imgView frame], [touch locationInView:self.sampleView])) {
            //if ([touch tapCount] == 2)
            //[imgView setupNextDisplayString];
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
            CGAffineTransform transform = CGAffineTransformMakeScale(1, 1);
            imgView.transform = transform;
            [UIView commitAnimations];
            return;
        }
    }
}

This Code is not for Zoom, Pan or Rotate. For these features you can go here
http://varunxcode.blogspot.in/2013/01/simple-drag-drop-with-zoom-and-rotate.html

No comments:

Post a Comment