[英]Swift -How to programmatically access collectionView's targetContentOffset.pointee.y
When a user swipes, I run some code in scrollViewWillEndDragging
and I need the targetContentOffset.pointee.y
to do it:当用户滑动时,我在
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...
}
The issue is I also do some autoscrolling, where the user doesn't have to swipe and the cv auto scrolls.问题是我也做了一些自动滚动,用户不必滑动并且 cv 自动滚动。 I still need access to the
targetContentOffset.pointee.y
so I can do something with with the visibleItem but I the method it's in doesn't trigger on autoscroll.我仍然需要访问
targetContentOffset.pointee.y
,所以我可以对 visibleItem 做一些事情,但它所在的方法不会在自动滚动时触发。
How can I programmatically access the targetContentOffset.pointee.y?如何以编程方式访问 targetContentOffset.pointee.y?
You can try你可以试试
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.