ExplodingTiger
ExplodingTiger

Reputation: 367

OnPointerHold interference with Scroll Rect scrolling

A simplified example of my problem - I used Unity UI to create the following:

enter image description here

A large panel, containing a list of smaller panels. I have attached a Scroll Rect to a container game object, thus making the smaller panels scrollable horizontally. No problem there, everything works as expected.

However, I have a script attached to each of the smaller panels, which implements the OnPointerHold interface. Now when I try to scroll through the smaller panels, the OnPointerHold event is fired, blocking the scrolling for some reason.

Any ideas as to why this happens?

My real case is that I need to have a scrollable list of images, which can be dragged onto the scene if the player taps & holds them. I'm using OnPointerHold to detect the tap & hold - if 0.7 seconds have passed, then let the player drag the image. However, I still want the player to be able to scroll through the list. I'm open to other suggestions on how to achieve that.

Thanks in advance, keep up the good work!

Upvotes: 2

Views: 2191

Answers (1)

Jerry Switalski
Jerry Switalski

Reputation: 2720

You need to implement only IPointerDownHandler and IPointerUpHandler interfaces. As long as you don't implement the drag or hold handlers, drag and hold events should pass right through to the Scroll Rect.

To use IPointerDownHandler and IPointerUpHandler for your purpose just count time from begining of void OnPointerDown (PointerEventData eventData) was called, and if no void OnPointerUp (PointerEventData eventData) is called in given time (0.7sec), move out the image from Scroll Rect and attach to some canvas, and update its position to pointer position.

Also this solution is good because if you start dragging the Scroll Rect, the void OnPointerUp (PointerEventData eventData) will be called which is good as, you want to drag Scroll Rect in given time (0.7sec).

Upvotes: 3

Related Questions