9

I have a UIScrollView that can be scrolled horizontally. The scrollable content is displayed in columns.

Is there a way to make the scrolling only move in stepped increments instead of pixels (i.e. move a column at a time).

Diagram: enter image description here

Camsoft
  • 11,718
  • 19
  • 83
  • 120

3 Answers3

7

Yup there is:

yourScrollView.pagingEnabled = YES;

Check the UIScrollView for more information.

If the value of this property is YES, the scroll view stops on multiples of the scroll view’s bounds when the user scrolls. The default value is NO.

I suggest you may need to change your view structure to something like this:

+--------+
|        | <- UIView to hold your structure
+--------+

   +--+
   |  |    <- UIScrollView, clipsToBounds = NO, width = column.width
   +--+

+--+--+--+
|  |  |  |  <- UIView columns
+--+--+--+

Center the UIScrollView horizontally and make sure that the UIView that contains the UIScrollView has a width that is a multiple of the width of the UIScrollView

Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • Also i believe iOS SDK 5 added support to "suggest" locations in a scroll view ? – Moszi Nov 23 '11 at 18:15
  • I can't see that in the docs :S Do you have a reference I can follow for that? – Paul.s Nov 23 '11 at 18:18
  • If I'm not mistaken would not not just show one column at a time, I want to show multiple columns like in my example. Also to make it clear I'm not that bothered it scrolls like normal as long as it always stops on a whole column. – Camsoft Nov 24 '11 at 09:29
  • It would show multiple columns as you set the `UIScrollView` to not clip to bounds therefore it's content can be seen outside of it's bounds – Paul.s Nov 24 '11 at 09:56
  • Also users expect a scrollview to behave like a scrollview, to say "I'm not that bothered it scrolls like normal" will most likely lead to a below standard UX. I've used the technique above in two of my apps and have been very happy with it. – Paul.s Nov 24 '11 at 10:18
  • Sorry misunderstood how it would work, that sounds exactly what I wanted. I'll give it a go and see how I get on. – Camsoft Nov 24 '11 at 11:16
  • I've tried to implement what you suggested but I'm finding that the user can only interact with the UIScrollView and if they try to scroll the part that is visible outside of the UIScrollView's bounds it does not respond to events (i.e. scroll). – Camsoft Nov 24 '11 at 11:49
  • 1
    Forward the hit messages from the containing `UIView` to the `UIScrollView`. – Paul.s Nov 24 '11 at 11:56
1

While I think paging is probably the right answer, it's not what you were asking, which was jerky scrolling (columns "at all times"). I believe you could provide a -scrollViewDidScroll: delegate and adjust contentOffset to the nearest column.

David Dunham
  • 8,139
  • 3
  • 28
  • 41
1

If you target iOS 5.0, you can use the new UIScrollViewDelegate method scrollViewWillEndDragging:withVelocity:targetContentOffset. This allows you to set the point where the scrolling animation will end after the user has stopped dragging the scroll view.

Dorian Roy
  • 3,094
  • 2
  • 31
  • 51