-1

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.

erdekhayser
  • 6,537
  • 2
  • 37
  • 69
  • 2
    Have you tried setting pagingEnabled = YES? https://developer.apple.com/library/ios/documentation/uikit/reference/uiscrollview_class/Reference/UIScrollView.html#//apple_ref/occ/instp/UIScrollView/pagingEnabled – Matías R Apr 11 '14 at 02:05
  • @MatíasR No I haven't. Thanks I'll look into it and get back to you – erdekhayser Apr 11 '14 at 02:23
  • 1
    @MatíasR Wow I actually feel dumb right now. If you post it as an answer I can accept it. – erdekhayser Apr 11 '14 at 02:25
  • And as it turns out, UIPageControl is not that hard to implement with this. – erdekhayser Apr 11 '14 at 02:50

1 Answers1

1

Set pagingEnabled = YES on your UIScrollView :)

cmd
  • 11,622
  • 7
  • 51
  • 61
Matías R
  • 2,195
  • 1
  • 14
  • 12