Search code examples
iosobjective-ccocoa-touchuiscrollviewuiscrollviewdelegate

Make UIScrollView act like UIPageControl


In my latest project, I am trying to create a UIScrollView with multiple sections that can be horizontally scrolled between. I want to make it so that after the user scrolls, the UIScrollView locks onto one section of the UIScrollView.

I have 3 distinct sections, and so to get the position where they all start, the code would be,

CGFloat contentWidth = scrollView.contentSize.width;
CGFloat firstDivider = 0; //left origin
CGFloat secondDivider = contentWidth; 
CGFloat thirdDivider = contentWidth*2;

I also have thought about using the center of each section to determine which should be brought to the front.

CGFloat centerXfirst = contentWidth/2;
CGFloat centerXsecond = contentWidth * 3/2;
CGFloat centerXthird = contentWidth * 5/2;

I have come to no success using either of these methods.

More Details

The UIScrollView's frame is created dynamically based on the other views around it. Its content size's width is 3 times the scroll view's width.

I think using a UIPageControl would be too much here, and I am definitely more familiar with the UIScrollView than the UIPageControl. This app is on a deadline so I cannot spend time learning this new UI element.


Solution

  • Set pagingEnabled = YES on your UIScrollView :)