在集合视图单元格的窗口中查找框架

Ber*_*lue 0 objective-c ios uicollectionview

我得到的所有可见细胞UICollectionViewController都是这样的.

NSArray<__kindof UICollectionViewCell *> *cells = [self.collectionView.visibleCells visibleCells];
Run Code Online (Sandbox Code Playgroud)

如何在窗口中找到单元格的框架?

iPr*_*abu 7

试试这个:

迅速

guard
    let indexPath = collectionView.indexPath(for: yourVisibleCell),
    let attributes = collectionView.layoutAttributesForItem(at: indexPath)
    else {
        assertionFailure("Can't get required attributes")
        return
}

let frameInWindow = collectionView.convert(attributes.frame, to: nil)
Run Code Online (Sandbox Code Playgroud)

目标C.

NSIndexPath *indexPath = [self.collectionView indexPathForCell:yourVisibleCell];

UICollectionViewLayoutAttributes *theAttributes = [collectionView layoutAttributesForItemAtIndexPath: indexPath];

CGRect cellFrameInSuperview = [collectionView convertRect:theAttributes.frame toView:nil];
Run Code Online (Sandbox Code Playgroud)

  • 将`nil`作为`toView:`参数传递给窗口的坐标.无需弄清楚正确的祖先观点. (4认同)