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

泪湿孤枕 提交于 2019-12-02 14:52:12

Use a UIScrollView with the pagingEnabled property set to YES. Typically a UIPageControl is used along with it.

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

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

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

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

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

It may help you.

Inherited UICollectionView.

Reuseable Cell, (likely Android's Adapter)

AND enable Preview part of left, right side.

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,

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

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

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

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

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".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!