IOS: drag and drop imageview from a scrollview to

2019-03-22 09:12发布

问题:

Possible Duplicate:
how to drag an uiimage from scrollview to another uiimageview in iphone sdk

In my iPad I have a view and inside it, on the left, I have a scrollview with 10 imageViews; so I should drag and drop these images from scrollview to my big subview; how can I do it?

回答1:

I did something like what you are describing once. I remember to pull it off I created a new UIGestureRecgonizer that I dubbed UIDownwardDragGestureRecognizer. I think I then iterated through the scrollviews gesture recognizers to require them to wait for the UIDownwardGestureRecognizer to fail like:

UIDownwardDragGestureRecognizer *downwardGesture = [UIDownwardGestureRecognizer alloc] initWithTarget:self action:@selector(downwardGestureChanged:)];
[myScrollview addGestureRecognizer:downwardGesture];
for (UIGestureRecognizer *gestureRecognizer in myScrollview.gestureRecognizers)
{
   [gestureRecognizer requireGestureRecognizerToFail:myDownwardGesture];
}

Once you have this setup, you should be able to do something like:

- (void) downwardGestureChanged:(UIDownwardDragGestureRecognizer*)gesture
{
   CGPoint point = [gesture locationInView:myScrollView];
   if (gesture.state == UIGestureRecognizerStateBegan) 
   {
      UIView *draggedView = [myScrollView hitTest:point withEvent:nil];
      if ([draggedView isTypeOfClass:[UIImageView class]])
      {
         self.imageBeingDragged = (UIImageView*)draggedView;
      }
   }
   else if (gesture.state == UIGestureRecognizerStateChanged)
   {
      self.imageBeingDragged.center = point;
   }
   else if (gesture.state == UIGestureRecognizerStateEnded     ||
            gesture.state == UIGestureRecognizerStateCancelled ||
            gesture.state == UIGestureRecognizerStateFailed)
   {
      // Determine if dragged view is in an OK drop zone
      // If so, then do the drop action, if not, return it to original location

      self.imageBeingDragged = nil;
   }
}

In the UIGestureRecognizerStateBegan, once you find a UIImageView you may want to remove it from the superview (scrollview) and add it as a subview to some other container. You will need to translate the point into the new coordinate space if you do this. If you want the original image to stay in the scrollview, then make a copy of it and add that to an outside container.

To try out the above example and see it working, you may need to turn off clipping on the scrollview since the example I gave above is dragging the UIImageView from inside the scrollview (though normally you'd add it to some containing view).



回答2:

On way I have seen this done is by using an overlay layer to capture the touches and pass them through. This allows you to intercept the clicks before the UIScrollView consumes them.

Here is the demo video that I looked at when trying to construct a UITableView (inside a scroll view) that could have cells dragged and dropped on to it.

Also I am not sure how applicable this is but here is a similar post I made a few days ago.

Having reviewed this issue some more I built something that allows you to drag and drop cells between UITableViews similar to the video I mentioned previously. My tutorial can be found here and the result can be seen in this YouTube video. This method uses the latest gesture features in iOS 5 and pops cells off using a long press.

Hope you can find something in here that helps.