How to create a horizontal carousel in iOS 10 Swift 3

I recently switched to iOS from the Android world. I saw two options for creating horizontal sliders. My goal is to create a horizontal slider of the cards present in this image. horizontal view cards I was confused what element is needed for the best option in iOS. Should I use CollectionView for this OR should I use PageViewController for this? Since I am starting, I cannot use the library at this stage with the limited knowledge that I have. Please suggest me how to do this.

+5
source share
1 answer

Short answer

You must use a UICollectionView.

More detailed answer

1) Customize the horizontal layout using UICollectionViewFlowLayout

 let layout = UICollectionViewFlowLayout() // The size of each item. Pick a suitable height so that the items do not get stacked: layout.itemSize = CGSize(width: view.frame.width / 2, height: view.frame.height) // The most important part: layout.scrollDirection = .horizontal // Then initialize collectionView let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout) 

2) Scaling the installation (pseudo-code)

I am. Add self method to UIScrollViewDelegate scrollViewDidScroll(UIScrollView)

II. Set collectionView.delgate = self

III. In scrollViewDidScroll(UIScrollView) get all visible cells by doing scrollView as! UICollectionView scrollView as! UICollectionView and getting the visibleCells property.

intravenously For a cell in visible cells, set the appropriate scale with CGAffineTransform specified cell distance from the horizontal center of the screen. This may lead to some arithmetic; this should work fine:

 s = −0.00005 * pow(cell.center.x, 2) + 1.5 

Hope this helps! Feel free to ask for clarification if you need it.

+7
source

Source: https://habr.com/ru/post/1270348/


All Articles