apptekstudios / ASCollectionView

A SwiftUI collection view with support for custom layouts, preloading, and more.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Layout orthogonalScrollingBehavior setting stopping onScroll()

fredz999 opened this issue · comments

Describe the bug
I setup the layout of an ASCollectionView with the following setting included body layout
section.orthogonalScrollingBehavior = .continuous

This achieves the effect of side scrolling for me, however when section.orthogonalScrollingBehavior is set at all my onScroll() seems to stop working

To Reproduce
import ASCollectionView and use the following as a view in a SwiftUI interface

struct SingleSectionExampleView: View {
@State var dataExample = (0 ..< 55).map { $0 }
var body: some View
{
ASCollectionView(data: dataExample, dataID: .self) { item, _ in
Color.blue
.overlay(Text("(item)").foregroundColor(.white))
}

    .layout {
        ASCollectionLayoutSection{layoutEnvironment in
            let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(30), heightDimension: .absolute(30))
            let item = NSCollectionLayoutItem(layoutSize: itemSize)
            item.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 1, bottom: 0, trailing: 1)
            let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5), heightDimension: .fractionalHeight(0.3))
            let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 3)
            let section = NSCollectionLayoutSection(group: group)
            section.orthogonalScrollingBehavior = .continuous
            return section
        }
    }
        
    .onScroll{a,b in
        print("a: ",a,", b",b)
    }
}

}

test the ui you have added this to in a simulator and then comment out the line
section.orthogonalScrollingBehavior = .continuous.
you should see the onScroll function work when the line is removed

Expected behaviour
A description of what you expected to happen.
I wanted the onScroll function to run while the ASCollectionView was scrolling sideways I eventually want to use the offset to programatically scroll more than one grid at once
Screenshots
If applicable, add screenshots to help explain your problem.

Xcode Version:

  • 11.4.1

Simulator, Device, Both?

  • Where is the problem occuring
    -Simulator -SE

Hi @fredz999,

This is due to the way apple's UICollectionViewCompositionalLayout works. You can actually define your own onScroll in your layout using NSCollectionLayoutSectionVisibleItemsInvalidationHandler

https://developer.apple.com/documentation/uikit/nscollectionlayoutsectionvisibleitemsinvalidationhandler

Note that it is called not only when scrolling, but also at other times:

The handler is called before each layout cycle, any time an animation occurs in that section due to changes such as adding or removing items, scrolling the section, or rotating the device.

Thanks for the quick response!
Tell me, the main two things Im trying to get from this grid control, are 1: the ability to alter the underlying data and have the UI changes automatically update(new cells and suchlike) and 2: use one controls translation width to alter other grids, is that handler the key to solving those two things?