![](/img/trans.png)
[英]Swift -how is a collectionView's scrollView.content.y determined?
[英]Swift -How to programmatically access collectionView's 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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.