【问题标题】:iOS page control scroll lockiOS页面控制滚动锁定
【发布时间】:2023-03-27 13:56:01
【问题描述】:

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

有没有办法在我在第一页时禁用向左滚动,或者在我在最后一页时禁用向右滚动?请在下面查看我的代码 sn-ps 以了解我在做什么。

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

【问题讨论】:

    标签: ios uiscrollview uipagecontrol


    【解决方案1】:

    我想通了。显然有一个'bounces' 属性允许用户在超出边界后继续滚动。关闭此功能会将窗口锁定到位。

    【讨论】:

      猜你喜欢
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 2012-06-25
      • 2023-03-30
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      相关资源
      最近更新 更多