1
votes

I have one collectionview with compositional layout, section 0 is a horizontal scrollable one with paging enabled (red circle on the picture).

So I use collectionView.scrollToItem(at: indexPath, animated: true) on collection view with a Timer to auto scroll section 0. But whenever the scroll happened, it automatically focus on section 0.

Is there any way to allow auto scroll on section 0, but prevent this section get focus while user maybe browse items further down of the collection view. Currently whenever auto scroll happen, it scroll onto the very top as well.

Here is the code fot setting up the compositional layout for collection view. section 0 is horizontal scrollable.

let compLayout = UICollectionViewCompositionalLayout { (sectionNumer, env) -> NSCollectionLayoutSection? in
        
        if sectionNumer == 0 {
            let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)))
            let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .estimated(280)), subitems: [item])
            let section = NSCollectionLayoutSection(group: group)
            section.orthogonalScrollingBehavior = .paging
            
            return section
        } else {
            let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(0.5), heightDimension: .absolute(200)))
            item.contentInsets.trailing = 8
            item.contentInsets.bottom = 8
            let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .estimated(500)), subitems: [item])
            group.contentInsets.leading = 8
            let section = NSCollectionLayoutSection(group: group)
            
            return section
        }
    }

   

Apologise if this has been answered, but I didn't find it. Appreciated for any help.

enter image description here

1

1 Answers

1
votes

I think you are asking: you want to show a different food item every few seconds, but the user could be looking farther down on the page, so when you try to show a different food item by telling the collection view to scroll to it, then it scrolls all the way back up to show the next food item. Is that right?

I think the solution is to disable the timer if that section isn't visible. You should also disable the timer if the user scrolled that section themselves recently, so that the timer doesn't undo what the user wanted to look at just now.