iOS: `UICollectionview`, how to get a cell's `contentoffset` by it's `indexPath`
Asked Answered
O

1

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!

Olnton answered 30/5, 2016 at 8:44 Comment(4)
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;Windpipe
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?Olnton
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.Windpipe
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.Olnton
O
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

Olnton answered 31/5, 2016 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.