iOS页面控制滚动锁
我有一个页面控件和一个滚动视图在我的视图之一中实现。目前我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。显然有一个“弹跳”属性,允许用户即使在超出边界后也可以继续滚动。关闭此功能会将窗口锁定到位。
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.