Search code examples
iosswiftmatrixscrollviewcollectionview

iOS / Swift | Infinite matrice of views : ScrollView or CollectionView?


I'm creating a infinite "matrix" of views. When I load my MainView, i create views around it. Then, if I scroll to the right view for example, I create new views around the right view.

I tried to schematize what I want to do :

enter image description here

I'm asking 2 questions :

1- Should I use ScrollView and add views in it OR a CollectionView and add collectionViewCells (with size of MainView) in it ? Which one is better and Why ? I was going to use ScrollView and views but CollectionView allows reusable cells which could be very powerful with infinite matrix ...

2- Since User can scroll Top and Left, How do you manage to add views / collectionViewCells at "index -1" without reloading or moving everything ?


Solution

  • Neither. Create your own UIView subclass which has a UIPanGestureRecognizer. When the pan is recognized, update the position of your child views. Add & remove children around the edges, so that you never have too many children. (As views move offscreen, you can removeFromSuperview).