在scrollToItemAtIndexPath之后获取UICollectionView单元格位置

Sho*_*cks 0 uikit ios uicollectionview

我想以UICollectionViewCell编程方式滚动到一个单元格后得到一个位置.我正在设置这样的滚动位置.

[self.collectionView scrollToItemAtIndexPath:indexPath
                             atScrollPosition:UICollectionViewScrollPositionCenteredVertically
                                        animated:NO];
Run Code Online (Sandbox Code Playgroud)

然后我想得到我滚动到的单元格的框架,因为我需要UIView在它附近放置另一个.

此代码返回带有scrollview的单元格的rect,但我需要在可见rect中的位置.

UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGRect cellRect = attributes.frame;
Run Code Online (Sandbox Code Playgroud)

jrt*_*ton 5

单元格的框架不会改变,它是封闭滚动视图的内容偏移量.contentOffset从帧的原点中减去以获得屏幕上的位置,或使用其中一种convertRect:方法获取相关视图中的坐标.