Skip to content

Instantly share code, notes, and snippets.

@mattorb
Created June 11, 2010 14:59
Show Gist options
  • Save mattorb/434586 to your computer and use it in GitHub Desktop.
Save mattorb/434586 to your computer and use it in GitHub Desktop.

Revisions

  1. mattorb revised this gist Jun 11, 2010. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions LazyLoopedScrollViewHooks.m
    Original file line number Diff line number Diff line change
    @@ -9,18 +9,16 @@ - (void)scrollViewDidScroll:(UIScrollView *)sender {

    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;
    UIViewController *controller;


    if (landingPage == 0 ) {
    [_scrollView scrollRectToVisible:CGRectMake(_pageSize.size.width*(_pageCount-2),0,_pageSize.size.width,_pageSize.size.height) animated:NO];
    controller = [_viewControllers objectAtIndex:_pageCount-2];
    } else if (landingPage == (_pageCount-1)) {
    [_scrollView scrollRectToVisible:CGRectMake(_pageSize.size.width*1,0,_pageSize.size.width,_pageSize.size.height) animated:NO];
    controller = [_viewControllers objectAtIndex:1];
    }
    }

  2. mattorb created this gist Jun 11, 2010.
    28 changes: 28 additions & 0 deletions LazyLoopedScrollViewHooks.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    - (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);
    }

    - (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;
    UIViewController *controller;

    if (landingPage == 0 ) {
    [_scrollView scrollRectToVisible:CGRectMake(_pageSize.size.width*(_pageCount-2),0,_pageSize.size.width,_pageSize.size.height) animated:NO];
    controller = [_viewControllers objectAtIndex:_pageCount-2];
    } else if (landingPage == (_pageCount-1)) {
    [_scrollView scrollRectToVisible:CGRectMake(_pageSize.size.width*1,0,_pageSize.size.width,_pageSize.size.height) animated:NO];
    controller = [_viewControllers objectAtIndex:1];
    }
    }