UIScrollView 水平分页,如 Mobile Safari 标签 [英] UIScrollView horizontal paging like Mobile Safari tabs
问题描述
Mobile Safari 允许你通过输入一种 UIScrollView 水平分页视图来切换页面,底部有一个页面控件.
Mobile Safari allows you to switch pages by entering a sort of UIScrollView horizontal paging view with a page control at the bottom.
我正在尝试复制这种特殊行为,其中水平滚动的 UIScrollView 显示下一个视图的一些内容.
I am trying to replicate this particular behavior where a horizontally scrollable UIScrollView shows some of the next view's content.
Apple 提供的示例:PageControl 展示了如何使用 UIScrollView 进行水平分页,但所有视图都占用了整个屏幕宽度.
The Apple provided example: PageControl shows how to use a UIScrollView for horizontal paging, but all views take up the whole screen width.
如何让 UIScrollView 像移动版 Safari 一样显示下一个视图的某些内容?
How do I get a UIScrollView to show some content of the next view like mobile Safari does?
推荐答案
启用分页的 UIScrollView
将在其框架宽度(或高度)的倍数处停止.所以第一步是弄清楚你希望你的页面有多宽.使其成为 UIScrollView
的宽度.然后,将子视图的大小设置为您需要的大小,并根据 UIScrollView
宽度的倍数设置它们的中心.
A UIScrollView
with paging enabled will stop at multiples of its frame width (or height). So the first step is to figure out how wide you want your pages to be. Make that the width of the UIScrollView
. Then, set your subview's sizes however big you need them to be, and set their centers based on multiples of the UIScrollView
's width.
然后,既然你想看到其他页面,当然要把clipsToBounds
设置为NO
,就像mhjoy所说的.现在的技巧部分是让它在用户开始拖动 UIScrollView
的框架范围之外时滚动.我的解决方案(当我最近不得不这样做时)如下:
Then, since you want to see the other pages, of course, set clipsToBounds
to NO
as mhjoy stated. The trick part now is getting it to scroll when the user starts the drag outside the range of the UIScrollView
's frame. My solution (when I had to do this very recently) was as follows:
创建一个 UIView
子类(即 ClipView
),它将包含 UIScrollView
及其子视图.本质上,它应该具有您认为 UIScrollView
在正常情况下会具有的框架.将 UIScrollView
放在 ClipView
的中心.确保 ClipView
的 clipsToBounds
设置为 YES
如果其宽度小于其父视图的宽度.此外,ClipView
需要对 UIScrollView
的引用.
Create a UIView
subclass (i.e. ClipView
) that will contain the UIScrollView
and it's subviews. Essentially, it should have the frame of what you would assume the UIScrollView
would have under normal circumstances. Place the UIScrollView
in the center of the ClipView
. Make sure the ClipView
's clipsToBounds
is set to YES
if its width is less than that of its parent view. Also, the ClipView
needs a reference to the UIScrollView
.
最后一步是在ClipView
中覆盖- (UIView *)hitTest:withEvent:
.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
return [self pointInside:point withEvent:event] ? scrollView : nil;
}
这基本上将 UIScrollView
的触摸区域扩展到其父视图的框架,这正是您所需要的.
This basically expands the touch area of the UIScrollView
to the frame of its parent's view, exactly what you need.
另一个选择是子类化 UIScrollView
并覆盖它的 - (BOOL)pointInside:(CGPoint) point withEvent:(UIEvent *) event
方法,但是你仍然会需要一个容器视图来进行裁剪,仅根据UIScrollView
的frame很难确定何时返回YES
.
Another option would be to subclass UIScrollView
and override its - (BOOL)pointInside:(CGPoint) point withEvent:(UIEvent *) event
method, however you will still need a container view to do the clipping, and it may be difficult to determine when to return YES
based only on the UIScrollView
's frame.
注意:您还应该查看 Juri Pakaste 的hitTest:withEvent: modify 如果您在子视图用户交互方面遇到问题.
NOTE: You should also take a look at Juri Pakaste's hitTest:withEvent: modification if you are having issues with subview user interaction.
这篇关于UIScrollView 水平分页,如 Mobile Safari 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!