1

I have a UICollectionView(One Row, Vertical align) with a UIPageControl associate with it.

The UIPageControl will have number of dots as the the number of items in my collection view.

I want to change/scroll the position of the collectionView when a dot is pressed. Moreover, I want change the selected dot according to the scrolling of the collection view.

Any idea about some kind of algorithm for the scrollViewDidScroll method too accomplish that?

Thanks in advance

iosMentalist
  • 3,066
  • 1
  • 30
  • 40
DaSilva
  • 1,358
  • 1
  • 19
  • 42
  • Possible duplicate of [iOS6 UICollectionView and UIPageControl - How to get visible cell?](http://stackoverflow.com/questions/13176333/ios6-uicollectionview-and-uipagecontrol-how-to-get-visible-cell) – iosMentalist Jan 18 '17 at 13:02

2 Answers2

4

I got a simple solution. Under the method scrollViewDidScroll just check the visible cell in the middle of the screen:

CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
[self.pageControl updateStateForPageNumber: (int)visibleIndexPath.row];
DaSilva
  • 1,358
  • 1
  • 19
  • 42
0

If I am understanding it correctly, I think you can do it via collectionView:cellForItemAtIndexPath: method of the UICollectionViewDataSource Whenever an item is requested from the data source you can select the relevant dot on UIPageControl. Did this help ?

vishalv2050
  • 813
  • 1
  • 10
  • 18
  • this not work, because when a cell is requested, it´s not showing on that moment, you have more visible area from the previous cell – DaSilva Jun 30 '14 at 17:36
  • @DaSilva Yes, but depending on how many cells you are showing on your screen at any moment you can get which cell is being shown in the "middle". Suppose you can show one cell at a time and you get a request for cell 3 then you know that the one that is currently visible is 2. – vishalv2050 Jun 30 '14 at 17:42
  • i think i find a better solution @vishalv2050. se my answer below – DaSilva Jul 01 '14 at 11:13