5

Possible Duplicate:
How do I enable directional lock for a UIScrollView?

Is there a way to have a UIScrollView perform paging e.g. only horizontally and not when scrolling vertically?

In other words, I want my scrollview to snap to page boundries when scrolling horizontally and free swiping when going vertically.

Of course the trivial solution is using two UIScrollViews, one in each direction, but this is not a feasible solution in my case.

Community
  • 1
  • 1
user1204271
  • 131
  • 1
  • 4
  • Yes, this is possible. http://stackoverflow.com/questions/861986/how-do-i-enable-directional-lock-for-a-uiscrollview – Paul Peelen May 07 '12 at 08:06
  • 1
    This only prevents scrolling in a certain direction. I want to allow scrolling in all directions, but only perform snap-to-boundries in horizontal – user1204271 May 07 '12 at 08:55
  • 1
    @casperone I don´t see how this is a duplicate of the marked question. This one is about paging. the other one about directional lock. – r-dent Dec 20 '17 at 11:08

1 Answers1

6

Presuming you always want snap if there is any movement in horizontal direction and free scrolling only if the horizontal offset has not changed.

  1. Set directionalLockEnabled = YES
  2. Wrap the UIScrollView in another view which forwards touches (as we will modify the scrollview's fram`e)
  3. Set the UIScrollView's frame height to 1 (in order to minimize vertical snapping when handling horizontal scroll)
  4. Detect the direction of the current swipe (in scrollViewDidScroll:)
  5. If x of contentOffset has changed, then set pagingEnabled = YES
  6. If only y of contentOffset has changed, then set pagingEnabled = NO

This will simulate the scenario you want. If you swipe horizontally the page will snap - and the vertical snap will be minimal due to the fame height being set to 1.
When swiping only vertically, the scrollview will scroll freely.

LK__
  • 6,515
  • 5
  • 34
  • 53