문제

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;
도움이 되었습니까?

해결책

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.

다른 팁

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top