orthogonalScrollingBehavior paging - getting current cell in center

I have a UICollectionView that is using groupPagingCentered orthogonalScrollingBehavior to show images in cells, almost one cell for the screen. I have a toolbar that can act on the current image. Because there seems to be no access to the scrollview, I have no way of telling where a user has paged to to act on it.

Code Block swift
+--------------------------------+
|-+ +------------------------+ +-|
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
|-+ +------------------------+ +-|
+--------------------------------+

Is there any way of getting what the user has paged to? (ie: the current centred cell after the user has moved to a new cell)

It seems that Scrollview delegates are not available when using orthogonalScrollingBehavior.

Accepted Reply

You can (and should) use the visibleItemsInvalidationHandler on orthogonal sections for the most accurate detail on your current scrolling context.

Code Block swift
let section = NSCollectionViewSection(group: group)
section.visibleItemsInvalidationHandler = { visibleItems, scrollOffset, environment in
/* ... use scrollOffset to determine the paging behaviour */
}
/* ...other setup */
let layout = UICollectionViewCompositionalLayout(section: section)

Replies

You can (and should) use the visibleItemsInvalidationHandler on orthogonal sections for the most accurate detail on your current scrolling context.

Code Block swift
let section = NSCollectionViewSection(group: group)
section.visibleItemsInvalidationHandler = { visibleItems, scrollOffset, environment in
/* ... use scrollOffset to determine the paging behaviour */
}
/* ...other setup */
let layout = UICollectionViewCompositionalLayout(section: section)

I was able to use the answer andy_ gave.

I stored my offset to a CGFloat variable.

Code Block Swift
section.visibleItemsInvalidationHandler = { [weak self] (visibleItems, scrollOffset, environment) in
   self?.currentScrollOffset = scrollOffset.x
}

I was then able to calculate the current cell being viewed by getting the cell width from the layout, taking into account insets, and calculating where the current scroll offset is. The index path I used was one known to exist, just for getting an accurate cell width.

Code Block Swift
let inset:CGFloat = 6
let indexPath = IndexPath(item: 0, section: 0)
if let attr = collectionView.layoutAttributesForItem(at: indexPath) {
  let cellWidth = attr.bounds.width
  let centerCell = cellWidth / 2
  let result = Int((currentScrollOffset + centerCell) / (cellWidth + inset))
  print("current index is \(result)")
}