2

I have been looking to all the other similar topics here, using UIGestureRecognizers, using hitTest:withEvent, pointInside:withEvent: etc. but nothing seems to be ok for what I need to achieve.

Basically I have a main view (self.view of a common UIViewController) and a small rectangular UIScrollView attached onto it at the bottom: the scrollView is filled with some UIImageViews and the user can scroll it as usual.

But the user should also be able to drag one UIImageView (or a copy of it) from the UIScrollView to the main view, and, this is what I am finding really difficult, with the SAME dragging gesture, hence I need a way to:

1) Distinguish between normal horizontal scrolling gesture, which should be handled by the UIScrollView the usual way and a dragging gesture over the image view.

2) Once identified a dragging gesture, should propagate the touch to the superview, which will host a copy of the UIImageView and WITH the SAME dragging gesture continue the dragging over the main view even out of the bounds of the UIScrollView.

Please note that I know that if the UIScrollView has userInteractionEnabled = NO the touch is propagated to the subviews, but 1) I want to propagate it to the superview not the subviews, 2) the userInteractionEnabled property apparently becomes active only once the initial gesture is terminated, while I need to use a single dragging gesture.

Thank you very much for any help.

Fabrizio Prosperi
  • 1,398
  • 4
  • 18
  • 32
  • You have to disable the delayContentTouches which is enabled by default, that's one thing. – nhahtdh Nov 24 '12 at 18:08
  • You mean to allow userInteractionEnabled = NO become immediately active? That's very good to know, doesn't solve the whole problem but might help, thank you so much. – Fabrizio Prosperi Nov 24 '12 at 18:11
  • I didn't mention anything about `userInteractionEnabled = NO`, it should be `YES` actually. As for the dragging, when your detect pan gesture has started on it, you create, or relocate, the object to the superview for the purpose of dragging; when gesture end is detected, then you put it into the appropriate subview with appropriate coordinates. – nhahtdh Nov 24 '12 at 18:16
  • the final location of the object is the superview, and the object should appear on the superview WHILE the dragging happens. Actually the dragging touch should only be detected by the scrollview but immediately "transferred" to the superview, which would immediately create a copy of the object (while the object on the scrollview will be deleted) and operate on it the dragging. Hope it is clearer now. I don't get how the delayContentTouches can do for me right now, can you explain it a little more now that you know better my target? Thanks. – Fabrizio Prosperi Nov 24 '12 at 18:27
  • The `delayContentTouches` set to `NO` will allow the object to catch the touch event as soon as it happen (otherwise, the scroll view will absorb the touch). The rest can be done as I described. Note that the touch will be detected by the view inside the scroll view, and you will modify the coordinate of the view accordingly. (Use the view that you touch and move it, while creating a new view under it). – nhahtdh Nov 24 '12 at 20:11
  • Thank you, that's clearer now, only I still don't think that, after the object will catch the dragging touch event, it will be able to either: 1) transfer the SAME dragging touch event to its copy on the superview, 2) transfer itself to the superview and still use the SAME dragging touch event to be moved onto it. – Fabrizio Prosperi Nov 25 '12 at 09:45
  • Apart from the first responder, the touch event will be sent to the most specific view first, so you don't have to worry about "transferring" anything. – nhahtdh Nov 25 '12 at 13:57

1 Answers1

2

So, so far I have ended up implementing the touchesShouldBegin:withEvent:inContentView: method of my UIScrollView subclass but with delayContentTouches set to YES (default) instead of NO as @nhahtdh was suggesting.

Strangely enough even only implementing the method was sufficient for my subviews to intercept the dragging, and still my scrollview is scrolling properly, while with delayContentTouches set to NO I was not able to scroll it as all the subviews were starting to move around.

Really the credit for this is @nhahtdh, so man, if you post an answer I will accept it, thank you very much for your help.

Fabrizio Prosperi
  • 1,398
  • 4
  • 18
  • 32