Scrolling a horizontal UIScrollView in stepped inc

2019-02-12 23:10发布

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

3条回答
劳资没心,怎么记你
2楼-- · 2019-02-12 23:31

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.

查看更多
倾城 Initia
3楼-- · 2019-02-12 23:38

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

查看更多
三岁会撩人
4楼-- · 2019-02-12 23:47

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.

查看更多
登录 后发表回答