Skip to content

Instantly share code, notes, and snippets.

@mattorb
Created June 11, 2010 14:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattorb/434586 to your computer and use it in GitHub Desktop.
Save mattorb/434586 to your computer and use it in GitHub Desktop.
- (void)scrollViewDidScroll:(UIScrollView *)sender {
BOOL isScrollingRight = _scrollView.contentOffset.x > _previousContentOffsetX;
_mostRecentScrollWasRight = isScrollingRight;
_previousContentOffsetX = _scrollView.contentOffset.x;
CGFloat pageWidth = _scrollView.frame.size.width;
int scrollingToPageNum = isScrollingRight ? (ceil((_scrollView.contentOffset.x - pageWidth) / pageWidth) + 1) : (floor((_scrollView.contentOffset.x -pageWidth) / pageWidth) + 1);
int percentOfScrollToPageOnscreen = isScrollingRight ? floor((((int)_scrollView.contentOffset.x % (int)pageWidth) / pageWidth)*100)
: floor((1 - ((int)_scrollView.contentOffset.x % (int)pageWidth) / pageWidth) * 100);
// load the controllers/views you deem appropriate based on having the above info
}
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView { //landed on a page
int landingPage = floor((self._scrollView.contentOffset.x - self._scrollView.frame.size.width / _pageCount) / self._scrollView.frame.size.width) + 1;
if (landingPage == 0 ) {
[_scrollView scrollRectToVisible:CGRectMake(_pageSize.size.width*(_pageCount-2),0,_pageSize.size.width,_pageSize.size.height) animated:NO];
} else if (landingPage == (_pageCount-1)) {
[_scrollView scrollRectToVisible:CGRectMake(_pageSize.size.width*1,0,_pageSize.size.width,_pageSize.size.height) animated:NO];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment