缩小UIScrollView以专注于特定页面 [英] Shrinking a UIScrollView to focus on a certain page
问题描述
我有一个带有水平分页的 UIScrollView
-与天气应用类似.在某些情况下,我想防止用户水平滚动.只是简单地禁用滚动会有点刺耳-我宁愿复制滚动到最后一页的体验-允许进行一点滚动,但是由于没有更多内容,滚动视图会弹回.
I have a UIScrollView
with horizontal pagination - similar to the weather app. Under certain circumstances, I want to prevent the user from scrolling horizontally. Simply disabling the scrolling is a little jarring - I would prefer to duplicate the experience of scrolling past the last page - a little scrolling is permitted, but since there is no more content, the scrollview bounces back.
我应该如何实施呢?我应该更改ScrollView的contentSize吗?如何设置分页,以便有效地存在一个页面",但仍在左,右两边显示内容?
How might I go about implementing this? Should I be changing the contentSize of the ScrollView? How do I set up the pagination so that there is effectively one 'page' but there is still content displayed to the immediate left and right?
推荐答案
为了始终使 UIScrollView
弹跳,即使只有一页,也请添加以下行:
In order to always make the UIScrollView
bounce, even with just one page, add the following line:
[self.scrollView setAlwaysBounceHorizontal:YES];
当然,除了我对缩小 contentSize
的评论.
In addition to my comment about shrinking the contentSize
, of course.
好的,让我先解释一下我们想要实现的目标.用户正在 UIScrollView
中的页面之间愉快地滚动,突然,我们希望 UIScrollView
坚持当前页面,第一页.
OK, let me first explain want we want to achieve. The User is scrolling happily between the pages in the UIScrollView
, and suddenly, we want the UIScrollView
to stick to the current page, which may or may not be the first page.
我们的 UIScrollView
具有页面布局,每个页面均由一个 UIView
或其子类表示(它可能具有子视图):
Our UIScrollView
has the pages laid out, with each page represented by a single UIView
or it's subclasses (it may have subviews itself):
现在,要执行所说明的任务,下面是代码:
Now, to perform the task explained, here is the code:
CGFloat pageWidth = _scrollView.bounds.size.width;
int currentPage = (_scrollView.contentOffset.x / pageWidth);
[_scrollView setContentSize:CGSizeMake(pageWidth, _scrollView.contentSize.height)];
// move each page to the left:
for (UIView* page in _scrollView.subviews) {
[page setFrame:CGRectOffset(page.frame, -currentPage * pageWidth, 0)];
}
注意::如果您具有复杂的视图层次结构,只需将其包装在一个 UIView
中,其边界大小等于 _scrollView.contentSize
包含所有页面.剩下的我留给读者练习(我一直想说xD)
NOTE: If you have a complicated view hierarchy, just wrap it in a UIView
that has a bounds size equal to the _scrollView.contentSize
with all the pages. The rest I leave it as an exercise for the reader (I always wanted to say that xD)
这篇关于缩小UIScrollView以专注于特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!