簡體   English   中英

Swift - 如何以編程方式訪問 collectionView 的 targetContentOffset.pointee.y

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM