简体   繁体   English

Swift - 如何以编程方式访问 collectionView 的 targetContentOffset.pointee.y

[英]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.

相关问题 Swift-如何确定collectionView的scrollView.content.y? - Swift -how is a collectionView's scrollView.content.y determined? Swift:如何以编程方式在 TableViewCell 中显示 CollectionView - Swift: How to display a CollectionView inside a TableViewCell Programmatically Swift iOS-如何为CollectionView的第二部分查找CGPoint x / y值 - Swift iOS -How to find CGPoint x/y values for CollectionView's second Section 以编程方式快速添加 CollectionView 项目 - Add CollectionView Items programmatically in swift 无限滚动CollectionView以编程方式快速 - Infinite Scroll in collectionView swift programmatically Swift:如何从Singleton类访问ViewController中的CollectionView - Swift: How to access CollectionView which is present in ViewController from a Singleton Class Swift - 如何在键盘抬起时滚动 collectionView 的内容 - Swift -How to scroll the collectionView's contents when the keyboard is raised UICollectionView的scrollToItem不调用targetContentOffset - UICollectionView's scrollToItem not calling targetContentOffset 如何以编程方式将CollectionView滚动到底部 - How to Scroll CollectionView to bottom Programmatically 如何以编程方式在collectionview中选择项目? - How to select item in collectionview programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM