UIScrollView with pagination + showing part of the previous/following pages(带有分页的 UIScrollView + 显示部分上一页/下一页)
问题描述
我正在尝试创建一种类似于割绳子"游戏用于选择关卡包的游戏模式"菜单:
I'm trying to create a kind of a "game mode" menu similar to the one used by the "Cut the Rope" game to select the level pack:
我特别想要的是实现显示当前项目"(在本例中为2. Fabric Box"项目)加上一些前后项目(以确保用户知道滚动有更多可用模式),启用分页(使滚动视图自动居中"在这些项目上).
What I want in particular is to achieve the same effect of showing the "current item" (in this case, the "2. Fabric Box" item) plus a bit of the previous and following items (to make sure the user is aware that there are more modes available by scrolling), with pagination enabled (to make the scroll view automatically "center" on these items).
对于启用分页的 UIScrollView 来说,这似乎是一项很自然的工作,但是从文档看来,分页发生在多个视图边界上.
This seems like a natural job for a UIScrollView with pagination enabled, however from the documentation it seems the pagination occurs on multiples of the view bounds.
那么:如果分页发生在多个视图边界上,有没有办法通过 UIScrollView 来实现这种效果?
So: if pagination occurs on multiples of the view bounds, is there any way to achieve this effect with a UIScrollView?
我们看到整个屏幕宽度的事实表明,在这种情况下 UIScrollView 框架的宽度将是 320 像素,但每个单独的项目都需要小于该宽度才能显示前一个和下一项,从而弄乱了分页...
The fact that we see the full width of the screen would suggest that the UIScrollView frame's width would be 320px in this case, but each individual item would need to be smaller than that in order to show that little bit of the previous and next items, thus messing up the pagination...
推荐答案
供您参考,您可以从此处查看页面控件的示例实现.https://developer.apple.com/library/content/samplecode/PageControl/Introduction/Intro.html
For your reference, you can see a sample implementation of a page control from here. https://developer.apple.com/library/content/samplecode/PageControl/Introduction/Intro.html
对于您想要的实现,令您惊讶的是,滚动视图的宽度实际上小于 320(或 480).要设置的魔法属性是:
For the implementation you want, to your surprise, the scrollview's width is actually smaller than 320 (or 480). The magic property to set is:
scrollView.clipsToBounds = NO
这个实现的唯一问题是,如果触摸超出了滚动视图的边界,滚动视图不会收到任何触摸事件.这可以通过将其父 hitTest 事件传递给 scrollView 来解决.
The only problem with this implementation is that the scrollview get no touch events if the touch is outside the bounds of the scrollView. This can be fix by passing its parent hitTest event to scrollView.
只是为了链接到更好的解释:UIScrollView水平分页,如移动Safari标签
Just to link to to a better explanation: UIScrollView horizontal paging like Mobile Safari tabs
与我推荐的略有不同,但作用相同.
Slightly different from what I recommend but does the same thing.
我有一个名为 LXPagingViews 的小项目,它可以执行上述操作,希望以开箱即用的方式(请给我一个拉取请求或问题反馈):https://github.com/lxcid/LXPagingViews
I have a small project called LXPagingViews that does the above, hopefully in an out of the box manner (Do drop me a pull request or feedback in issue): https://github.com/lxcid/LXPagingViews
这篇关于带有分页的 UIScrollView + 显示部分上一页/下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有分页的 UIScrollView + 显示部分上一页/下一页
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01