Equivalent to the Android's ViewPager on iOS?

2020-05-11 21:08发布

问题:

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.

What is the equivalent to this on iOS?

回答1:

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



回答2:

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



回答3:

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



回答4:

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.



回答5:

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