iOS页面控制滚动锁

发布于 2025-01-05 05:04:45 字数 1369 浏览 0 评论 0原文

我有一个页面控件和一个滚动视图在我的视图之一中实现。目前我有 2 个页面,滚动视图内容大小设置为宽度 496(每个页面为 248)。一切工作正常,因为它更新和滚动正确,但是,我注意到即使我的左侧或右侧没有页面,我也可以继续滚动。

有没有办法在我位于第一页时禁用向左滚动,或者在最后一页时禁用向右滚动?请参阅下面的代码片段以了解我在做什么。

// 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 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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So要识趣 2025-01-12 05:04:46

我想通了。显然有一个“弹跳”属性,允许用户即使在超出边界后也可以继续滚动。关闭此功能会将窗口锁定到位。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文