【问题标题】:Swift -How to programmatically access collectionView's targetContentOffset.pointee.ySwift - 如何以编程方式访问 collectionView 的 targetContentOffset.pointee.y
【发布时间】:2021-05-17 09:36:14
【问题描述】:

当用户滑动时,我在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?

【问题讨论】:

  • 你如何自动滚动?
  • 竖屏,全屏,我用这个:collectionView.scrollToItem(at:IndexPath(item: xyz, section: 0), at: .centeredVertically, animated: true)

标签: ios swift uicollectionview uiscrollview


【解决方案1】:

你可以试试

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)
  }
}

【讨论】:

  • 感谢您的帮助。我不需要访问单元格,我需要访问单元格在 cv 中的框架/位置。当我使用let yAxis = targetContentOffset.pointee.y; let visibleItem = Int(yAxis / collectionView.frame.height) 时,我会从那里得到它。
猜你喜欢
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 2019-12-10
  • 2015-09-03
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多