Skip to content

Instantly share code, notes, and snippets.

@Lejdborg
Created May 7, 2012 08:39
Show Gist options
  • Save Lejdborg/2626688 to your computer and use it in GitHub Desktop.
Save Lejdborg/2626688 to your computer and use it in GitHub Desktop.
Updated UIPageControl for paged UIScrollView
/**
* Set the UIScrollView's delegate to self, and set self's protocol to UIScrollViewDelegate.
*
* This works with a horizontally paged scroll view but can be easily converted to vertical,
* just switch all "width" to "height".
*/
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = _scrollView.frame.size.width;
int currentPage = ceil((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth);
_pageControl.currentPage = currentPage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment