Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling a horizontal UIScrollView in stepped increments?

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

like image 699
Camsoft Avatar asked Nov 23 '11 18:11

Camsoft


3 Answers

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

like image 186
Paul.s Avatar answered Nov 10 '22 00:11

Paul.s


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.

like image 1
David Dunham Avatar answered Nov 10 '22 00:11

David Dunham


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.

like image 1
Dorian Roy Avatar answered Nov 10 '22 01:11

Dorian Roy