简体   繁体   中英

iOS page control scroll lock

I have a page control and a scrollview implemented in one of my views. At the moment I have 2 pages and a scrollview content size set to a width of 496 (each page being 248). Everything works well as it updates and scrolls properly, however, I am noticing that I can continue scrolling even when there is no page to the left or right of me.

Is there a way to disable scrolling to the left if I'm on the first page or disable scrolling on the right if I'm on the last page? Please see my code snippets below to see what I am doing.

// Initialize the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 2, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;

// Functions called for the page control/scrollview
- (void)loadScrollViewWithPage:(int)page window:(UIView *)pageView
{
    if(page < 0 || page >= pageControl.numberOfPages)
    {
        return;
    }

    // Add our view
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [pageView setFrame:frame];
    [scrollView addSubview:pageView];
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    // Update our page when we have more than 50% of the adjacent page available
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    if(page < 0 || page >= pageControl.numberOfPages)
    {
        return;
    }

    pageControl.currentPage = page;

    [pageControl setNeedsDisplay];
}

I figured it out. Apparently there was a 'bounces' property that allows the user to continue scrolling even after they go past the bounds. Turning this off locks the window in place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM