Author Topic: UIScroolView Only drag one direction if it fits  (Read 2671 times)

hrlarsen

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 35
    • View Profile
UIScroolView Only drag one direction if it fits
« on: March 13, 2014, 12:16:36 PM »
Hi

I am currently trying to make an app which updates content when you drag it downwards. I am using a ScrollView but cannot figure out how to go around making it behave like you see in example Facebook or twitter apps on mobile when you pull down for refreshing content. Currently it works fine when the content is larger than the scrollview, but when it it does not fit I run into problems.

The scrollview do reset itself to have content in the top when dragging down, I tried using UICenterOnChild, but that didnt work since it just fits it the the center of it, and I want the top. Secondly I tried using ResetPosition, but it just snaps right to it, and do not go about it smoothly. I also tried implementing it myself, by lerping the position of the scrollview to move towards 0, and when dragging upwards i set custom movement to vector2.zero, but this was clunky and did not work proberly.

So anyone has a solution to this? I would think it would be a nice feature to have some options like "Keep Content To Origin if fits" and maybe a "Cancel Drag in certain direction if fits". :)

The attached image shows that the scrollview just "hangs" there and do not pull back up the the content origin (the text says: "pull down to refresh" in danish)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIScroolView Only drag one direction if it fits
« Reply #1 on: March 14, 2014, 12:32:00 PM »
UIScrollView.SetDragAmount sets the relative position. UIScrollView.ResetPosition() resets it to the content pivot point -- whatever your scroll view's pivot point was set to be. If you want it to go to a specific point and do it smoothly, use SpringPanel.Begin -- but you will need to calculate the position using some math. UICenterOnClick uses it, so you can use it as an example.

hrlarsen

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 35
    • View Profile
Re: UIScroolView Only drag one direction if it fits
« Reply #2 on: March 19, 2014, 11:55:18 AM »
I see, thanks will try fiddling with that

hrlarsen

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 35
    • View Profile
Re: UIScroolView Only drag one direction if it fits
« Reply #3 on: March 19, 2014, 01:00:03 PM »
I ended up doing a solution that looks like this, just wanted to share. (could possibly be improved)

  1. if(!scrollView.shouldMoveVertically){
  2.         scrollView.dragEffect = UIScrollView.DragEffect.None;
  3.         if(scrollView.isDragging){
  4.                 scrollView.customMovement = Vector2.up * 0.5f; // slow drag
  5.                 scrollView.movement = UIScrollView.Movement.Custom;
  6.                 scrollView.DisableSpring();
  7.         }
  8.         else{
  9.                 scrollView.movement = UIScrollView.Movement.Vertical;
  10.                 SpringPanel.Begin(scrollView.panel.cachedGameObject, Vector3.zero, 20); // jump to the top gamobject
  11.         }
  12. }
  13. else
  14.         scrollView.dragEffect = UIScrollView.DragEffect.MomentumAndSpring;
  15.