如何解决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)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。