Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to handle UIPanGestureRecognizer and moving your all images w.r.t moving image.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 755
    Comment on it

    The following code help your image to move using UIPanGestureRecognizer. While moving your selected image other images set there location automatically.

    -(void)addImageToScrollView
    {
            float x=5,y=40,width = 100,height = 100,spaceH = 5,spaceW = 5;
            for (int i = 1; i <= imageArray.count; i++) {
    
                [scrollView addSubview:[self getImageView:CGRectMake(x, y, width, height) forImageName:[imageArray objectAtIndex:i-1] forTag:i]];
    
    
                if (i%3 == 0 ) {
                    x = 5;
                    y = y+height+spaceH;
    
    
                }else{
                    x = x+width+spaceW;
                }
            }
    
            [scrollView setContentSize:CGSizeMake(320, 1000)];
    
     }
    
    
    -(CustomView *)getImageView:(CGRect)frame forImageName:(NSString *)imageName forTag:(int)tag{    
            CustomView *imageView = [[CustomView alloc]initWithFrame:frame];
            imageView.delegate = self;
            [imageView.imageView setImage:[UIImage imageNamed:imageName]];
            imageView.tag = tag;
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanImage:)];
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:pan];
            return imageView;
    }
    
    - (void)handlePanImage:(UIPanGestureRecognizer *)sender
    {
        static CGPoint originalCenter;
        static CGPoint originalCenter2 ;
    
        static CGPoint newCenter;
        if (sender.state == UIGestureRecognizerStateBegan)
        {
            originalCenter2 = sender.view.center;
            originalCenter = sender.view.center;
            sender.view.alpha = 0.8;
            [sender.view.superview bringSubviewToFront:sender.view];
        }
        else if (sender.state == UIGestureRecognizerStateChanged)
        {
            CGPoint translation = [sender translationInView:self.view];
    
            sender.view.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
            UIView *view = [self checkImageContained:sender.view.tag];
            if (view) {
                newCenter = view.center;
                originalCenter2 =[self shiftImage:sender.view.tag tilTag:view.tag frame:originalCenter2 uiview:sender.view];
            }else{
                UIView *view = [self checkImageContainedinReverse:sender.view.tag];
                if (view) {
                    newCenter = view.center;
                    originalCenter2 =[self shiftImagereverse:sender.view.tag tilTag:view.tag frame:originalCenter2 uiview:sender.view];
                }
            }
    
    
        }
        else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed)
        {
            [UIView animateWithDuration:0.2 animations:^{
                sender.view.alpha  = 1.0;
            }];
        }
    }
    
    -(UIView *)checkImageContained:(int)tag{
        CustomView *view = (CustomView*)[scrollView viewWithTag:tag];
        for (int i = 1; i <= imageArray.count; i++) {
            if (tag == i) {
                continue;
            }
    
            CustomView *vv = (CustomView*)[scrollView viewWithTag:i];
    
    
            if (CGRectContainsRect(vv.frame, view.frame)) {
                return vv;
            }
            if (vv.tag>view.tag) {
                float x = vv.center.x - view.center.x;
                float y = vv.center.y - view.center.y;
                if (x<20&& x>-20 && y<20 && y>-20) {
                    return vv;          
                }
            }
    
        }
            return nil;
    }
    -(UIView *)checkImageContainedinReverse:(int)tag{
        CustomView *view = (CustomView*)[scrollView viewWithTag:tag];
        for (int i = 1; i <= imageArray.count; i++) {
            if (tag == i) {
                continue;
            }
    
            CustomView *vv = (CustomView*)[scrollView viewWithTag:i];
            if (vv.tag<view.tag) {
    
    
                NSLog(@"vv %f %f %f %f",vv.center.x,vv.center.y,view.center.x,view.center.y);
                float x = vv.center.x - view.center.x;
                float y = vv.center.y - view.center.y;
                if (x<20&& x>-20 && y<20 && y>-20) {
                    return vv;
    
    
                }
            }
    
           }
    
        }
        return nil;
    }
    
    
    -(CGPoint)shiftImage:(int)startTag tilTag:(int)endTag frame:(CGPoint)previousFrame uiview:(UIView *)originalView{
    
            CGPoint nextFrame;
            CGPoint pp = previousFrame;
            if (startTag < endTag) {
                for (int i = startTag+1; i<=endTag; i++) {
                    UIView *view = (UIView *)[scrollView viewWithTag:i];
    
                    nextFrame = view.center;
    
                    view.tag = i-1;
    
                    [UIView animateWithDuration:0.2 animations:^{
                    view.center = pp;
                    }];
                    pp = nextFrame;
                }
                originalView.center = pp;
                originalView.tag  = endTag;
            }
        return pp;
    }
    -(CGPoint)shiftImagereverse:(int)startTag tilTag:(int)endTag frame:(CGPoint)previousFrame uiview:(UIView *)originalView
    {  
        CGPoint nextFrame;
        CGPoint pp = previousFrame;
        if (startTag > endTag) {
            for (int i = startTag-1; i>=endTag; i--) {
                UIView *view = (UIView *)[scrollView viewWithTag:i];
    
                nextFrame = view.center;
    
                view.tag = i+1;
    
                [UIView animateWithDuration:0.2 animations:^{
                    view.center = pp;
                }];
                pp = nextFrame;
            }
            originalView.center = pp;
            originalView.tag  = endTag;
        } 
        return pp;
    }
    

 0 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: