Paging UIScrollView with different page widths
Asked Answered
T

1

17

I would like to have a horizontal scrolling UIScrollView with paging enabled. The pages in this scrollview have different widths, so the scrolling distance differs from page to page.

The goal is to make a picker for different points in time, e.g.:

|  Now  |  Yesterday evening |  Last Week  |  Last Month  |
    ^              ^                ^              ^           <- stopps here

Here | Now | has a smaller width than | Yesterday evening |. When paging through this values, the scrollview should stop at the center of the according value.

Is that possible?

Tact answered 23/2, 2011 at 10:17 Comment(1)
See my answer on this question: https://mcmap.net/q/374070/-uiscrollview-custom-pagingAbradant
S
13

it's surely possible, but not so automatically...

i guess you should implement the UIScrollViewDelegate protocol method:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

}

it's the method called when the user stop to move the finger on the scrollView, you can check inside it the coordinate of your content:

yourScrollView.contentOffset

and then check which one of your page.x ( register them in an array when you create them, or check the origin of all your view added to the scrollView) is closer to it, then go to the offSet of your page (with animation) calling:

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
Septuple answered 23/2, 2011 at 10:39 Comment(2)
Thank you meronix, that was exactly what i was looking for! Some additional tips: - UIScrollView.pagingEnabled has to be set to NO - I also had to implement the UIScrollViewDelegate protocol method: - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollViewTact
for a better result i ended implementing the - scrollViewWillBeginDecelerating:(UIScrollView *)scrollView methodRainmaker

© 2022 - 2024 — McMap. All rights reserved.