ios 相当于iOS上的Android的ViewPager?滑动/翻阅数据页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9734995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:21:23  来源:igfitidea点击:

Equivalent to the Android's ViewPager on iOS? Swipe/flip through pages of data

ioslayoutswipe-gesture

提问by Vanja

On Android there's neat way to browse between several pages of data with the same layout by using ViewPager and Fragments. User swipes left or right, and the pages change. There's only one view controller and one layout resource for all pages, but the data content changes when the user browses. There's also an animation effect to the swiping left and right where you can see the content of the next page is already loaded.

在 Android 上,使用 ViewPager 和 Fragments 可以轻松地在具有相同布局的多个数据页面之间进行浏览。用户向左或向右滑动,页面会发生变化。所有页面只有一个视图控制器和一个布局资源,但是当用户浏览时数据内容会发生变化。左右滑动也有动画效果,可以看到下一页的内容已经加载。

What is the equivalent to this on iOS?

在 iOS 上与此等效的是什么?

回答by Kristopher Johnson

Use a UIScrollViewwith the pagingEnabledproperty set to YES. Typically a UIPageControlis used along with it.

使用UIScrollViewpagingEnabled属性设置为YES。通常 aUIPageControl与它一起使用。

If you Google for "UIScrollView paging", you can find many tutorials and examples. Apple provides a PageControlsample program.

如果您在 Google 上搜索“UIScrollView paging”,您可以找到许多教程和示例。Apple 提供了一个PageControl示例程序。

iOS doesn't provide any sort of adapter-like class, but here is a tutorial that explains how to implement "virtual pages" so that you don't have to instantiate all the pages at once: http://cocoawithlove.com/2009/01/multiple-virtual-pages-in-uiscrollview.html

iOS 不提供任何类似适配器的类,但这里有一个教程,解释了如何实现“虚拟页面”,这样您就不必一次实例化所有页面:http: //cocoawithlove.com/ 2009/01/multiple-virtual-pages-in-uiscrollview.html

回答by Ingolmo

You should look at UIPageViewController. Its available since iOS 5.

你应该看看UIPageViewController。它从 iOS 5 开始可用。

回答by Shabib

I've created a custom control for iOS which acts similar to android's viewPager. You can check this out here.

我为 iOS 创建了一个自定义控件,它的作用类似于 android 的 viewPager。你可以在这里查看

回答by hyunwoo

visit https://github.com/vowed21/HWViewPager

访问https://github.com/vowed21/HWViewPager

It may help you.

它可能会帮助你。

Inherited UICollectionView.

继承了 UICollectionView。

Reuseable Cell, (likely Android's Adapter)

可重复使用的 Cell,(可能是 Android 的适配器)

AND enable Preview part of left, right side.

并启用左侧,右侧的预览部分。

回答by DragonCherry

You can implement it your own using following idea.

您可以使用以下想法自己实现它。

Suppose that we wish to infinitely loop through 3 items(cells) - C0, C1, C2, we can generate dummy cells at the left and right side of center cells, the result as follows,

假设我们希望无限循环遍历3个items(cells)——C0,C1,C2,我们可以在中心单元格的左右两侧生成虚拟单元格,结果如下,

C0 C1 C2 [C0 C1 C2] C0 C1 C2

C0 C1 C2 [C0 C1 C2] C0 C1 C2

Cells in bracket are cells we see through device screen, and if we scroll to left,

括号中的单元格是我们通过设备屏幕看到的单元格,如果我们向左滚动,

C0 [C1 C2 C0] C1 C2 C0 C1 C2

C0 [C1 C2 C0] C1 C2 C0 C1 C2

at this moment, force contentOffset to point right side of given dummy cells,

此时,强制 contentOffset 指向给定虚拟单元格的右侧,

C0 [C1 C2 C0] C1 C2 C0 C1 C2 -> C0 C1 C2 C0 [C1 C2 C0] C1 C2

C0 [C1 C2 C0] C1 C2 C0 C1 C2 -> C0 C1 C2 C0 [C1 C2 C0] C1 C2

It will works in same mechanism when you scroll it to right.

当您向右滚动时,它将以相同的机制工作。

In my solution below, setting item view width to equal to its parent view will solve your problem.

在我下面的解决方案中,将项目视图宽度设置为其父视图将解决您的问题。

https://github.com/DragonCherry/HFSwipeView

https://github.com/DragonCherry/HFSwipeView

If you just wanna simply check how it works, click link below and "tap to play".

如果您只是想简单地检查一下它是如何工作的,请单击下面的链接并“点击播放”。

https://www.cocoacontrols.com/controls/hfswipeview

https://www.cocoacontrols.com/controls/hfswipeview

Just using pagingEnabled option in UIScrollView may work in case of full-sized cell item, but it will not work properly if you want to set narrower width of content item than its parent(scroll) view. Please check this feature by referring to cocoacontrols sample for "Sync" or "Edge Preview".

仅在 UIScrollView 中使用 pagingEnabled 选项可能适用于全尺寸单元格项,但如果您想设置比其父(滚动)视图更窄的内容项宽度,它将无法正常工作。请参考 cocoacontrols 示例中的“同步”或“边缘预览”来检查此功能。