I'm using a UICollectionView with a flow layout to show a list of cells, I also have a page control to indicate current page, but there seems to be no way to get current index path, I know I can get visible cells:
UICollectionView current visible cell index
however there can be more than one visible cells, even if each of my cells occupies full width of the screen, if I scroll it to have two halves of two cells, then they are both visible, so is there a way to get only one current visible cell's index?
Thanks
In Collectionview-> cellForItemAtIndexPath (Method) add the below code for calculate the Number of pages,
Add the ScrollView Delegate method,
You can get the current index by monitoring contentOffset in scrollViewDidScroll delegate
it will be something like this
I had similar situation where my flow layout was set for UICollectionViewScrollDirectionHorizontal and I was using page control to show the current page.
I achieved it using custom flow layout.
/------------------------ Header file (.h) for custom header ------------------------/
/------------------- Implementation file (.m) for custom header -------------------/
Your class containing collection view must conform to protocol 'CustomViewFlowLayoutDelegate' I described earlier in custom layout header file. Like:
There are two ways to hook your custom layout to collection view, either in xib OR in code like say in viewDidLoad:
Last thing, in MyCollectionViewController implementation file, implement delegate method of 'CustomViewFlowLayoutDelegate'.
}
I hope this would be helpful. :)
for swift 4.2
you can skip the function _SetTimer() , thats for auto scroll
Definitely you need catch the visible item when the scroll movement is stopped. Use next code to do it.
With UICollectionViewDelegate methods