IOS: drag and drop image from scroll to view

Possible duplicate:
how to drag 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 have to drag these images from scrollview into my big subview; How can i do this?

+2
source share
2 answers

I did something like what you describe once. I remember to take it off, I created a new UIGestureRecgonizer , which I called UIDownwardDragGestureRecognizer . I think I then repeated through the scrollviews of gesture recognizers so that they wait for the UIDownwardGestureRecognizer fail, for example:

 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 setting, you can 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 UIGestureRecognizerStateBegan, as soon as you find the UIImageView, you can remove it from the supervisor (scrollview) and add it as a spy to some other container. If you do this, you will need to translate the point into the new coordinate space. If you want the original image to remain in scrollview, make a copy of it and add it to the external container.

In order to try out the above example and see how it works, you may need to disable cropping on scrollview, as the above example drags the UIImageView out of the scroll (although you usually add it to some containing view).

+4
source

On the way, I saw how this was done using the overlay layer to capture the strokes and transfer them. This allows you to intercept clicks before UIScrollView uses them.

Here is a demo video that I was looking at while trying to create a UITableView (inside a scroll) that could drag and drop cells and rushed towards it.

Also I'm not sure how applicable this is, but here is a similar article that I did a few days ago.

Having looked at this problem, I created something that allows me to drag cells between UITableViews, similar to the video I mentioned earlier. 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 resets cells with a long press.

Hope you can find something here that helps.

+1
source

Source: https://habr.com/ru/post/1381529/


All Articles