UIScrollView horizontal paging like Mobile Safari tabs(UIScrollView 水平分页,类似于 Mobile Safari 选项卡)
问题描述
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:
创建一个包含 UIScrollView
及其子视图的 UIView
子类(即 ClipView
).本质上,它应该具有您认为 UIScrollView
在正常情况下应该具有的框架.将 UIScrollView
放在 ClipView
的中心.如果 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:
.
The final step is to override - (UIView *)hitTest:withEvent:
inside the ClipView
.
- (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
的框架可能很难确定何时返回 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 选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIScrollView 水平分页,类似于 Mobile Safari 选项卡
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01