如何获取 UICollectionViewCell 的矩形? [英] How to get the rect of a UICollectionViewCell?

查看:17
本文介绍了如何获取 UICollectionViewCell 的矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UITableViewrectForRowAtIndexPath: 方法,但在 UICollectionView 中不存在此方法.我正在寻找一种干净的方法来获取单元格的边界矩形,也许我可以将其添加为 UICollectionView 上的类别.

UITableView has the method rectForRowAtIndexPath:, but this does not exist in UICollectionView. I'm looking for a nice clean way to grab a cell's bounding rectangle, perhaps one that I could add as a category on UICollectionView.

推荐答案

我发现的最佳方法如下:

The best way I've found to do this is the following:

Objective-C

UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];

斯威夫特

let attributes = collectionView.layoutAttributesForItem(at: indexPath)

然后您可以通过 attributes.frameattributes.center

Then you can access the location through either attributes.frame or attributes.center

这篇关于如何获取 UICollectionViewCell 的矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆