UICollectionView CompositionalLayout not calling UIScrollDelegate

code snippet:

section.visibleItemsInvalidationHandler = { [weak self] visibleItems, point, environment in
    self?.pager.currentPage = visibleItems.last!.indexPath.row
}

The answer is using the visibleItemsInvalidationHandler closure on the section (NSCollectionLayoutSection).

This handler receives updates whenever an event results in an animation. It operates similar to scrollViewDidScroll and on horizontal scrolling groups it will be passed all the items, the scroll view offset and the NSCollectionLayoutEnvironment.

https://developer.apple.com/documentation/uikit/nscollectionlayoutsection/3199096-visibleitemsinvalidationhandler

Example:

section.visibleItemsInvalidationHandler = { [weak self] visibleItems, point, environment in
    self?.currentIndex = visibleItems.last?.indexPath.row
}

Appears to be an unexpected behaviour of horizontal scrolling groups within UICollectionView compositional layout

In case someone else encounters the same issue. It seems to be an issue with horizontal scrolling compositional layout groups.

only vertical scrolling groups will trigger the UIScrollViewDelegates.

Unfortunately, it also means that it seems decelerationrate.fast cannot be applied to a horizontal scroll. It will just be ignored.

Steps to reproduce the issue and analysis

This behavior can be recreated by implementing UIScrollViewDelegate methods in the wwdc example code "Advancements in Collection View Layout" here: https://developer.apple.com/videos/play/wwdc2019/215/ In the class: OrthogonalScrollBehaviorViewController.swift we find horizontal and vertical scrolling groups.

Conclusion

UIScrollViewDelegate only interacts with vertical scrolling groups. Horizontal scrolling groups do not communicate with the collection view's scroll delegate.

If there is a need for scroll delegate methods in horizontal scrolling groups than a traditional approach with nested CollectionViews and FlowLayout / Custom layout is still needed.

Remarks

If someone can point out to me that I am missing something I'd be very grateful until then this will stand as the answer to my above-stated issue.

Thanks to all who have commented.


UICollectionViewDelegate will call the scrollDidView if you vertically scroll the collectionView. In my situation ,I trying to track from horizontal scroll, I'm using this following code to make things work.

func collectionView(_ collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
      let visibleRect = CGRect(origin: yourCollectionView.contentOffset, size: photoCollectionView.bounds.size)
      let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
      let visibleIndexPath = yourCollectionView.indexPathForItem(at: visiblePoint) // Current display cell in collectionView
}

It's not the answer for this question, but hope this help person who searching for tracking current display cell from horizontal scroll in collectionView compositional layout