1
votes

I have UIScrollView contain many UIImageViews. I need to drag an UIImage from UIScrollView to another view. But my code drags only in UIScrollView.I can't drag an image to outside the scrollView . Please help me.. My code is..

for (int i = 0; i < [myArray count]; i++) {
  image = [[UIImageView alloc]initWithFrame:CGRectMake(0, y, 75, 30)];
  image.userInteractionEnabled = YES;
  image.backgroundColor = [UIColor blueColor];

  UIPanGestureRecognizer *panRecg = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(labelDragged:)];
  [image addGestureRecognizer:panRecg];
  y=y+35;

  [gScrollView addSubview:image];
}

-(void)labelDragged:(UIPanGestureRecognizer *)recognizer {
  UIImageView *imageView = (UIImageView *)recognizer.view;
  CGPoint newCenter = [recognizer translationInView:self.view];

  if([recognizer state] == UIGestureRecognizerStateBegan) {     
    beginX = imageView.center.x; 
    beginY = imageView.center.y;
  }

  newCenter = CGPointMake(beginX + newCenter.x, beginY + newCenter.y);

  [imageView setCenter:newCenter];
}
3
Hmm interesting question. Try setClipsToBounds:NO on your scroll view and see if it makes a difference.Ege Akpinar
before coding, how do you want the app to know the difference in user intent between dragging one of the subviews and dragging in order to scroll? maybe use two finger drag for dragging the subview?danh
@EgeAkpinar If you turn off clipping, even if that worked, it would be problematic because once it's "off" the scroll view, you presumably don't want it to move as the scroll view scrolls. Bottom line, you just want to remove it from the scrollview and add it to the view on to which you're dropping it.Rob
how come, when framing the subviews, y advances 35px in the loop, but the views are 75px wide? if you left space in between them, maybe we could say that dragging in that empty space == scrolling?danh
@danh , Y just used for adding more than one images into scroll view.I used verical scroll view..IKKA

3 Answers

2
votes

A couple of observations:

  1. When dragging it off the scrollview, you want to remove it from the scroll view and add it to, for example, self.view. You'll then do addSubview and adjust the coordinates accordingly:

    newCenter = [self.view convertPoint:newCenter fromView:imageView.superview];
    [self.view addSubview:imageView];
    imageView.center = newCenter;
    
  2. Make sure to set clipsToBounds for the scroll view to NO, so you can continue to see it as you drag it off.

  3. Note, I've changed this to refer to imageView.superview rather than the scroll view. If you keep scrollview references in there, you have to add conditional checks to make sure that the image view hasn't already been moved off the scroll view.

1
votes

I have a couple UI questions for you in comments, but @Rob is correct that the key to giving the appearance of moving the view around the hierarchy is to actually move it within the hierarchy. When dragging begins...

CGRect newFrame = [gScrollView convertRect:imageView.frame toView:self.view];
imageView.frame = newFrame;
[self.view addSubview:imageView];

The gesture recognizer should remain intact.

1
votes

Here is way to drag

-(void)labelDragged:(UIPanGestureRecognizer *)recognizer {

{
        UIImageView *imageView=(UIButton *)[recognizer view];
        CGPoint newCenter = [recognizer translationInView:self.view];
        if (recognizer.state==UIGestureRecognizerStateBegan) {
            CGRect rect=[self.view convertRect:[imageView frame] fromView:[imageView superview]];
            beginX = imageView.center.x; 
            beginY = imageView.center.y;
            [vew setFrame:rect];
            [self.view addSubview:vew];
        }
        else
        {
            CGRect rect=imageView.frame;
            newCenter.x=(newCenter.x+Begin.x);
            newCenter.y=(newCenter.y+Begin.y);
            imageView.center=newCenter;
            if (CGRectIntersectsRect(rect, [AcceptingView frame])) {
                [AcceptingView setBackgroundColor:[UIColor lightGrayColor]];//Notifying that the AcceptingView will accept the icon
            }
            else
            {
                //change back to old color
            }
        }
        if (recognizer.state==UIGestureRecognizerStateEnded)
        {
            CGRect rect=[imageView frame];
            if (CGRectIntersectsRect(rect, [AcceptingView frame])) {
                  //your method of adding the Image to acceptView
                 CGRect rect=[acceptView bounds];
                 rect.size=imageView.frame.size;
                 imageView.frame=rect;
                 [AcceptingView addSubView:imageView];
            }
            else//else part is means for if user drop the dragged view somewhere else other than AcceptingView
            {
                   //add image back to scrollView
            }
        }
    }