iPhone - 嵌套 UIScrollViews 以进行水平分页和垂直滚动

发布于 2024-09-08 19:32:13 字数 590 浏览 7 评论 0原文

我正在开发我的第一个 iPhone 应用程序,非常感谢你们对我遇到的问题提供意见。

我正在寻求实现水平和垂直滚动。我希望水平滚动进行分页,而不进行垂直滚动(“正常”滚动)。 pagingEnabled 设置为 YES 的单个 UIScrollView 将在两个方向上分页。自然的解决方案是将一个 UIScrollView 嵌套在另一个 UIScrollView 中,但是当我这样做时,我根本无法让“内部”UIScrollView 滚动。似乎外部事件正在“吞噬”所有点击事件,例如:

UIScrollView :水平分页,垂直滚动?

我读到了一些关于 SDK 3.0 中改进的“内部滚动”的内容,实际上,当我添加内部 UITableView 而不是 UIScrollView 时,滚动可以完美地工作。由于 UITableView 是 UIScrollView 的子类,我想我想要的行为应该可以通过创建自己的 UIScrollView 子类来实现。

这是正确的方法吗?如果是这样,这个子类应该是什么样子?

I'm developing my first iPhone app and I would greatly appreciate you guy's input on a problem I'm having.

I'm looking to implement scrolling both horizontally and vertically. I want the horizontal scrolling to be paged, without the vertical one being paged (scrolling "normally"). A single UIScrollView with pagingEnabled set to YES will page in both directions. The natural solution would be to nest a UIScrollView inside another one, however when I do that, I can't get the "inner" UIScrollView to scroll at all. Seems the outer one is "eating" up all the tap events, like in:

UIScrollView : paging horizontally, scrolling vertically?

I read something about "inner scrolling" being improved upon in SDK 3.0 and actually when I add an inner UITableView instead of a UIScrollView the scrolling works flawlessly. Since UITableView subclasses UIScrollView I imagine that my desired behavior should be achievable by making my own subclass of UIScrollView.

Is this the right approach? If so, what should this subclass look like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何以心动 2024-09-15 19:32:13

现在,这可以通过 SDK 开箱即用。请参阅疯狂滚动Apple 的 UIPageControl 示例,了解如何使用每个页面的视图控制器实现分页水平滚动的指南。

作为子视图添加到外部 UIScrollView 的嵌套 UIScrollView 应具有与容器相同的框架高度。如果这样做,那么外部 UIScrollView 将通过垂直滚动事件传递到子视图。我的应用程序具有三层 UIScrollView 和 UIWebView 嵌套,我发现 Cocoa 非常智能,只要我设置帧大小,就可以将事件传递给我想要的事件,以便只有一个视图真正可滚动(contentSize > frame)在每个轴上。

This works out of the box with the SDK now. See Scrolling Madness and Apple's UIPageControl sample for guidelines on how to implement paged horizontal scrolling with a view controller for each page.

The nested UIScrollViews you add as subviews to your outer UIScrollView should have the same frame heights as the container. If you do this then the outer UIScrollView will pass through the vertical scrolling events to the subview. My app has three levels of UIScrollView and UIWebView nesting and I've found Cocoa is really intelligent about passing the events to the one I want as long as I set my frame sizes so that only one view is really scrollable (contentSize > frame) on each axis.

平定天下 2024-09-15 19:32:13

如果你正在尝试像 Twitter 个人资料 UI 这样的东西,我通过将标题视图和底部滚动视图放在父滚动视图中并在后面放置另一个滚动视图来实现这一点。

底层scrollview负责调整header和bottom的内容偏移量。它的内容大小也根据内部项目的高度进行调整。

说起来好像很复杂,最好看代码
https://github.com/OfTheWolf/Twitterprofile

If you are trying something like Twitter profile UI I achieved this by putting header view and bottom scrollview in a a parent scrollview and underlaying another scrollview behind.

Underlaying scrollview is responsible for the adjusting content offsets of header and bottom. Its contentsize is also adjusted by the inner item heights.

It looks complicated when I tell, better see the code
https://github.com/OfTheWolf/Twitterprofile

踏雪无痕 2024-09-15 19:32:13

我已经使用 Andrey Tarantsov 这个很棒的库几个月了: SoloComponents

您可以将其用作“水平 UITableView”,支持分页和视图回收。
制作精良,完美可可风格的设计。

I've been using this great lib by Andrey Tarantsov for months: SoloComponents

You can use it as an "horizontal UITableView" with support for pagination and view recycling.
Well made and perfectly cocoa-style designed.

dawn曙光 2024-09-15 19:32:13

根据 Dylan 的回答,就我而言,实际上我还必须使父级 UIScrollView 和嵌套 UIScrollView 的内容大小高度相等,以使嵌套 UIScrollView 垂直滚动。正如迪伦解释的那样,仅制作“与容器相同的框架高度”是不够的:

parentScrollView.contentSize = CGSizeMake(parentScrollView.contentSize.width, desiredContentHeight);
nestedScrollView.contentSize = CGSizeMake(nestedScrollView.frame.size.width, desiredContentHeight);

Based on Dylan's answer, in my case, I actually also had to make content size heights equal for both parent and nested UIScrollViews to make nested UIScrollView to scroll vertically. Making only "the same frame heights as the container" as Dylan explained was not enough:

parentScrollView.contentSize = CGSizeMake(parentScrollView.contentSize.width, desiredContentHeight);
nestedScrollView.contentSize = CGSizeMake(nestedScrollView.frame.size.width, desiredContentHeight);
南城追梦 2024-09-15 19:32:13

迪伦所说的。并且,也许对此主题感兴趣 - 您不需要在主“分页”UIScrollView 中启用滚动,只需启用分页和方向锁定。这似乎可以确保所有垂直滚动提示都进入嵌套的、固定大小的垂直滚动 NSScrollView。它至少可以追溯到 iOS 9。

What Dylan says. And, perhaps of interest on this topic - you do not need to enable scrolling in the master "paging" UIScrollView, just enable paging and direction lock. This seems to assure that all vertical scrolling cues go to the nested, fixed-size, vertical-scrolling NSScrollViews. It works back to at least iOS 9.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文