How to drag uiimage from scrollview to another uiimageview in iphone sdk

In my application, I have one scrollview containing multiple images.

and from scrollview I have one uiimageview view.

I want to drag any image from a ScrollView and drop it onto a uiimageview that is out of scroll.

Is it possible?

help And suggestions are welcome

Thanks in advance.

+3
source share
3 answers

Yes, it should be possible, you will need to implement touch / drag events directly.

Pay attention to touchhesBegan, touchsMoved, etc. delegation methods in UIResponder.

One approach would be to subclass imageview and implement touchhesBegan, touchhesMoved in it, and use this subclass of imageview to display your scroll images.

On touchhesBegan, create a new image and add it to the look and set its image to the same as the one displayed in the scroll. You need to overlay it directly on the original image in the scroll, so adjust its beginning of the frame relative to the appearance, you will need to use the beginning of the scroll, as well as the size of the content view and the image offset of the original image inside the content to recount the new origin in appearance.

Then, by touching, move, just adjust the frame of this image in accordance with the coordinates of the movements that are moved so that the image follows the touch.

Check the border with the frame of your target image - as soon as the user drags it onto this border, make the image of the target image the same as the image in the drag view, and remove the drag image from the containing view and release it.

+2
source

gamozzii reply is close to what you need to do, but there is one problem. UIScrollView will use touches, so clicking on an image will have no effect. As a result, you will have to subclass UIScrollView

I wrote a small functional application to illustrate dragging an image from a scroll view into an image view. You can download the project here.

+4
source

If you are still interested in another solution (and for other users, of course):

I implemented this behavior before I recommended gamozzii.

I set canCancelContentTouches = NO in the UIScrollView to make sure the subviews handlers are touching each other. If the subview was affected (in your case, the image), I moved the view from scroll to the supervisor and started tracking its drag and drop. (You must calculate the correct coordinates in the new supervisor so that it remains in place). After dragging the ends, I checked if the target area was reached, otherwise I moved it back to the scroll.

Subsections handle drag and drop on their own via ( touchesBegan:/Moved:/Ended:/Cancelled: .

If this is not detailed enough, here is my sample code: Github: JDDroppableView

+1
source

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


All Articles