Scrolling a horizontal UIScrollView in stepped inc

2019-02-12 23:34发布

问题:

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:

回答1:

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



回答2:

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.



回答3:

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.