9

I'm using a UICollectionview with a circular layout. I'm trying to calculate the contentOffsetper item but with the circular layout the full contentsize does not seem match the content.

  • The collectionview's total contentSize = 780
  • The content offset of the last item = 397(?)
  • The content offset per item = 33(?)

Is there any way I can get the offset for an item by it's indexPath or at least the correct (397) value for the last item's in the collectionview? I got these values by testing and printing the contentoffset but I would like to calculate these numbers (33 and 397) by code without having to scroll.

So is there a way to calculate a cell's contentoffset(?) inside the collectionview by it's indexPath? Thanks!

Steven B.
  • 1,143
  • 1
  • 12
  • 32
  • 1
    Maybe you can get cell's frame and by knowing that calculate it's content offset? UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath]; CGRect cellRect = attributes.frame; – Josip B. May 30 '16 at 08:48
  • ok, I have the x,y,width and height of my cell..but I want the content offset of the item inside my uicollectionview. what code would I use? – Steven B. May 30 '16 at 12:02
  • ContentOffset tells you how much scrollView has been scrolled in some direction if scrollView's contentSize is greater than it's frame.size, otherwise contentOffset is 0. If you want to translate that to position of UICollectionViewCell it would be something like: CGFloat offsetY = fabs(self.collectionView.contentOffset.y - cellLayoutAttributes.frame.origin.y); but I don't know why you need that. To be honest I'm not sure it that is right logic because UICollectionViewCell cannot have contentOffset, it has frame and indexPath. – Josip B. May 30 '16 at 12:27
  • I know the cell only has a frame and indexpath but I need the offsets for the scrollView per cell. the big project is that i have 2 collectionviews, I want to transform the pan gesture from the first collectionview to the second collectionview whose full contentSize is a lot smaller than the first collectionview. I want to find a multiplier between these 2 contentSizes but... my contentSize value is incorrect so i would like to calculate the contentOffset for 1 cell and multiply it with the number of items in the collectionview. I hope you understand. – Steven B. May 30 '16 at 12:53

1 Answers1

2

I found the problem, the contentsize of the scrollview is the size of the content + the size of the uicollectionview's frame. That was the reason the contentSize returned a larger size than the actual content was.

so: (contentSizeX - collectionviews frame X) / cells = contentOffset per item (780 - 383) / 12 => 33

Steven B.
  • 1,143
  • 1
  • 12
  • 32