Author Topic: Limiting ScrollView movement  (Read 9107 times)

Holy Manfred

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 8
  • Posts: 71
    • View Profile
Limiting ScrollView movement
« on: October 25, 2016, 05:09:24 PM »
I have a scroll view with a UITexture as a child which can be dragged around using a UIDragScrollView. Basically a 2D map for the player to drag around on. Works as expected.

I would like to cancel the dragging movement when one side of my map texture reaches the edges of the scroll view. Example: Player drags map from left to right -> Cancel drag when left end of the map texture reaches the left edge of my scroll view.
I was going to check the position when dragged, then cancel when the edges hit the screen boundaries. Not really that difficult, however I am not sure if there isn't already some magic checkbox or anything built into NGUI that takes care of that problem?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Limiting ScrollView movement
« Reply #1 on: October 30, 2016, 12:00:54 PM »
There is though? It's specified on the scroll view. Restrict within panel = true, cancel drag if fits = true, drag effect = none.

Holy Manfred

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 8
  • Posts: 71
    • View Profile
Re: Limiting ScrollView movement
« Reply #2 on: October 30, 2016, 01:43:27 PM »
Yes that is right, but what I meant was that I want my UITexture to never leave the boundaries of the scrollview, even while dragging. With the settings you described the content will always snap back to fit, but it can temporarily be dragged further than intended before doing so.
In my scripts I am currently using
  1. myPanel.ConstrainTargetToBounds(myContent.transform,true);

to make sure nothing ever leaves my scrollview. This does the job, however I am not sure if that is the intended way to use it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Limiting ScrollView movement
« Reply #3 on: November 03, 2016, 07:58:33 AM »
With the 3 settings set the way I described you won't be able to drag the content past the scroll view's bounds at all. It will stop when it hits the edge.

Holy Manfred

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 8
  • Posts: 71
    • View Profile
Re: Limiting ScrollView movement
« Reply #4 on: November 03, 2016, 09:06:44 AM »
I would think so too, but I just tested in a clean project with an empty scene. With the options set as mentioned I am still able to drag elements out of my scrollview. They do snap back, but will temporarily leave the scrollview.
Even the scrollview example scene that ships with NGUI can be used to reproduce the behavior. Or alternatively a setup like this:

UIRoot
- ScrollView (Restrict = true, Cancel Drag = true, Effect = None)
--UITexture (BoxCollider and UIDragScrollView)