ios - Swift - 如何以编程方式访问 collectionView 的 targetContentOffset.pointee.y
问题描述
当用户滑动时,我会运行一些代码,scrollViewWillEndDragging
我需targetContentOffset.pointee.y
要这样做:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let yAxis = targetContentOffset.pointee.y
let visibleItem = Int(yAxis / collectionView.frame.height)
// do something with visibleItem based on cell's current vertical scroll position...
}
问题是我也做了一些自动滚动,用户不必滑动并且 cv 自动滚动。我仍然需要访问,targetContentOffset.pointee.y
所以我可以对 visibleItem 做一些事情,但我它所在的方法不会在自动滚动时触发。
如何以编程方式访问 targetContentOffset.pointee.y?
解决方案
你可以试试
let index = IndexPath(item: xyz, section: 0)
collectionView.scrollToItem(at:index, at: .centeredVertically, animated: true)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
if let cell = collectionView.cellForItem(at:index) {
let frame = collectionView.convert(cell.frame,nil)
}
}
推荐阅读
- c# - 秒表返回一个刻度
- mysql - Postgresq / Python,将更新和插入查询变成一个查询,仅当记录不存在时才插入
- api - IIS 中 swagger api 端点上的 Windows 身份验证
- python - Python 3 和 Django 2 更快地加载或复制对象以供查看?
- swift - Xcode 调试器不显示变量的值?
- azure-data-factory - Azure 数据工厂 - “变量”不是公认的函数
- python - python 3 pycrypto iv 必须为 16 字节长
- android - 创建新片段时,我可以在捆绑包上使用设置器吗
- c# - 如何有效地调整图像的亮度
- sql - 为什么将除数设置为 0 时没有出现除法错误?