The frame of a cell doesn't change, it's the content offset of the enclosing scroll view. Subtract the contentOffset
from the frame's origin to get the on-screen position, or use one of the convertRect:
methods to get the coordinates in a related view.
Get UICollectionView cell position after scrollToItemAtIndexPath
-
20-07-2023 - |
Pregunta
I would like to get a UICollectionViewCell
's position after programattically scrolling to a cell. I'm setting the scroll position like this.
[self.collectionView scrollToItemAtIndexPath:indexPath
atScrollPosition:UICollectionViewScrollPositionCenteredVertically
animated:NO];
I would then like to get the frame of the cell I have scrolled to as I need to position another UIView
near it.
This code returns the rect of the cell with the scrollview, but I need the position within the visible rect.
UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGRect cellRect = attributes.frame;
Solución
Otros consejos
Here is the cell X and Center values
UICollectionViewLayoutAttributes *attributes = [collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGPoint cellCenterPoint = attributes.center;
CGPoint contentOffset = [collectionView contentOffset];
NSLog(@"%f , %f",-1*contentOffset.x , cellCenterPoint.x);
int relativeCellCenter = (-1*contentOffset.x) + cellCenterPoint.x +collectionView.frame.origin.x;
int relativeX = (-1*contentOffset.x) +collectionView.frame.origin.x;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow