简体   繁体   English

如何防止自动聚焦在集合视图组合布局 swift

[英]How to prevent auto focus on collection view compositional layout swift

I have one collectionview with compositional layout, section 0 is a horizontal scrollable one with paging enabled (red circle on the picture).我有一个具有组合布局的集合视图,第 0 部分是启用分页的水平滚动视图(图片上的红色圆圈)。

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.所以我在集合视图上使用collectionView.scrollToItem(at: indexPath, animated: true)并使用 Timer 来自动滚动第 0 部分。但每当滚动发生时,它会自动关注第 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.有没有办法允许在第 0 节上自动滚动,但在用户可能浏览收藏视图下方的项目时防止此部分获得焦点。 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.第 0 部分是水平滚动的。

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.感谢任何帮助。

在此处输入图像描述

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.如果用户最近自己滚动了该部分,您还应该禁用计时器,以便计时器不会撤消用户刚才想要查看的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM