Skip to content

Instantly share code, notes, and snippets.

@Eridana
Created March 3, 2016 12:04
Show Gist options
  • Select an option

  • Save Eridana/5e6d75ca68b4969e970e to your computer and use it in GitHub Desktop.

Select an option

Save Eridana/5e6d75ca68b4969e970e to your computer and use it in GitHub Desktop.

Revisions

  1. Eridana created this gist Mar 3, 2016.
    50 changes: 50 additions & 0 deletions Scroll view paging
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #pragma mark - UIScrolViewDelegate

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
    CGFloat pageWidth = scrollView.frame.size.width;
    float fractionalPage = scrollView.contentOffset.x / pageWidth;

    NSInteger page = lround(fractionalPage);

    if (page == 0) {
    [self setFirstPageSelected];
    }
    if (page == 1) {
    [self setSecondPageSelected];
    }
    if (page == 2) {
    [self setThirdPageSelected];
    }

    if (self.currentPage == 2 && fractionalPage > 2.0) {
    [self showRegister];
    }
    }

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
    [self stoppedScrolling:scrollView];
    }

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
    if (!decelerate) {
    [self stoppedScrolling:scrollView];
    }
    }

    - (void)stoppedScrolling:(UIScrollView *)scrollView
    {
    CGFloat pageWidth = scrollView.frame.size.width;
    float fractionalPage = scrollView.contentOffset.x / pageWidth;

    self.currentPage = lround(fractionalPage);
    }

    #pragma mark - Paging

    - (void)scrollToPage:(int)page
    {
    [self.scrollView setContentOffset:CGPointMake(self.view.width * page, 0) animated:NO];
    }