Author Topic: Forcing Scroll View to update  (Read 7075 times)

Cwal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Forcing Scroll View to update
« on: April 21, 2014, 04:49:55 PM »
I have a scroll view that opens and closes panels on click, which means that the size of the scroll view changes at runtime.  I am having trouble getting the scroll view to update it's position so that when things are opened and closed, things stay properly bounded.  When the last item is closed, everything remains in the furthest scrolled position, so half of the view is empty.  I can't seem to find a way to force an update that will get it to move properly.  I have tried UpdatePosition, but it doesn't seem to work.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Forcing Scroll View to update
« Reply #1 on: April 22, 2014, 04:41:41 AM »
UIScrollView.ResetPosition() is what you're looking for.

Cwal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Forcing Scroll View to update
« Reply #2 on: April 22, 2014, 01:38:31 PM »
ResetPosition completely resets the view.  I need to stay scrolled to a specific object but remove the empty space generated by centering the object. When an object near the end of the content is centered, the view scrolls past the end and doesn't slide back so that it is properly constrained.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Forcing Scroll View to update
« Reply #3 on: April 23, 2014, 05:21:59 AM »
In that case, UIScrollView.RestrictWithinBounds is the function to call.

Cwal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Forcing Scroll View to update
« Reply #4 on: April 24, 2014, 02:38:44 PM »
  1. public void Toggle()
  2.     {
  3.        
  4.         bool hasHappened = false;
  5.         bool thisTime = opened;
  6.         if (content != null)
  7.         {
  8.             Vector3 thisAdjustment = adjustmentSize;
  9.         foreach (TabSlider tab in tabs)
  10.         {
  11.             //Debug.Log(tab.name);
  12.             if (hasHappened)
  13.             {
  14.                 tab.transform.localPosition = tab.transform.localPosition + thisAdjustment;
  15.             }
  16.             if (tab.opened)
  17.             {
  18.                 hasHappened = true;
  19.                 thisAdjustment = tab.adjustmentSize;
  20.             }
  21.             tab.opened = false;
  22.             if (tab.content != null)
  23.             {
  24.                 tab.content.gameObject.SetActive(false);
  25.             }
  26.         }
  27.         opened = !thisTime;
  28.         Debug.Log(opened);
  29.         if (content != null)
  30.         {
  31.             content.gameObject.SetActive(opened);
  32.         }
  33.         foreach (TabSlider tab in tabs)
  34.         {
  35.             if (opened && tab.tabNumber > tabNumber)
  36.             {
  37.                 Debug.Log("Moved");
  38.                 tab.transform.localPosition = tab.transform.localPosition - adjustmentSize;
  39.             }
  40.             else
  41.             {
  42.                 Debug.Log("Not Moved");
  43.             }
  44.            
  45.         }
  46.     }
  47.         view.UpdatePosition();
  48.         view.RestrictWithinBounds(true);
  49.         StartCoroutine(Late());
  50.        
  51.     }
  52.     IEnumerator Late()
  53.     {
  54.         yield return new WaitForEndOfFrame();
  55.         if (view)
  56.         {
  57.             Debug.Log("Restricting");
  58.             view.RestrictWithinBounds(true);
  59.            
  60.         }
  61.     }
Here is the code I am currently using to resize and move the elements of the scroll view.  I am using a modified version of your Center script to center on a child of the clicked item, then after that movement is done, I call this to properly open and close the menus.  Then I call RestrictWithinBounds, but the menus still end up scrolled part way out of bounds when at the edges.  If the menu doesn't have a child, it is properly restricted, but when a child is added, it breaks. It also fails if Toggle is called on its own OnClick, which is how I initially set it up.

If the RestrictWithinBounds call is put into a coroutine and called a second later to ensure that the tweening has finished, it jumps to the position it should be in and then slides back to the center.

What does the instant bool change?  It isn't in the documentation.
« Last Edit: April 24, 2014, 03:07:04 PM by Cwal »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Forcing Scroll View to update
« Reply #5 on: April 25, 2014, 09:04:14 AM »
Widgets don't add themselves to panels immediately on instantiation. There is a delay to allow for things like re-parenting and modifying properties. In your case you enable the objects, but you never actually update the widgets before you try to restrict the content. So the content is technically not there yet. Either wait until the end of frame before restricting, or call the Update() function on widgets and Refresh() on panels before you do your restriction.

Cwal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Forcing Scroll View to update
« Reply #6 on: April 28, 2014, 12:33:02 PM »
Is there an alternative to SpringPanel to do the centering?  It appears to be the tweening that SpringPanel.Begin causes that is breaking it.  If I yield for a second it will snap to the right place, otherwise it snaps for one frame and slides back to its original position.

Making SpringPanel instantaneous I still have problems with the bottom of the scroll view, even with several cycles of WaitForEndOfFrame followed by refreshing the panel.  Updating the widgets doesn't seem to have an effect either.  It definitely seems that the clipping for the scroll view isn't updating, no matter how long I wait.
« Last Edit: April 28, 2014, 07:57:53 PM by Cwal »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Forcing Scroll View to update
« Reply #7 on: April 29, 2014, 12:51:05 PM »
SpringPanel simply goes to a specific point. Does this point get set before everything is re-centered? Or perhaps it gets set, and then you change the panel's position? Try disabling or just removing the SpringPanel when you do repositioning.