SWIFT:如何修复单元格荧光笔并使整个集合视图滚动而不是荧光笔?

Posted

技术标签:

【中文标题】SWIFT:如何修复单元格荧光笔并使整个集合视图滚动而不是荧光笔?【英文标题】:SWIFT: How to fix cell highlighter and make the whole collection view scroll instead of the highlighter? 【发布时间】:2020-09-18 14:25:28 【问题描述】:

我正在构建一个 TVOS 应用,其中有这个 collectionView当前单元格(滚动到的单元格)以橙色突出显示。 例如,这里用户滚动到第三个单元格: 我想要达到的目标: 当用户滚动到另一个单元格时,我希望 橙色方块 保留在 第一个单元格 中,并且 整个集合 滚动到向左(如果用户向相反方向滚动,则向右滚动。 老实说,我不知道如何实现这一点,或者我应该使用什么。 我应该将整个集合视图嵌入到滚动视图中吗? 无论如何,以下是该集合视图的实现方式:

extension MoviesViewController2: UICollectionViewDelegate, UICollectionViewDataSource 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
            print("first function collectionView detected")
            return items2.count
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
            cellIdentifier, for: indexPath) as! movieCardCell
              cell.movieImageView.sd_setImage(with: URL(string: items2[indexPath.item].imageURL))
            return cell
    
    
    // Handle collectionViewItem selection
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
        print("didSelectItem\(indexPath)")
    
    
    // Highlight the current cell
    func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) 
        if let pindex  = context.previouslyFocusedIndexPath, let cell = collectionView.cellForItem(at: pindex) 
            cell.contentView.layer.borderWidth = 0.0
            cell.contentView.layer.shadowRadius = 0.0
            cell.contentView.layer.shadowOpacity = 0.0
        
        
        if let index  = context.nextFocusedIndexPath, let cell = collectionView.cellForItem(at: index) 
            cell.contentView.layer.borderWidth = 8.0
            cell.contentView.layer.borderColor = UIColor.orange.cgColor
            cell.contentView.layer.shadowColor = UIColor.orange.cgColor
            cell.contentView.layer.shadowRadius = 10.0
            cell.contentView.layer.shadowOpacity = 0.9
            cell.contentView.layer.shadowOffset = CGSize(width: 0, height: 0)
            collectionView.scrollToItem(at: index, at: [.centeredHorizontally, .centeredVertically], animated: true)
        
    

如果有人能回答上述问题,那就太好了。也欢迎任何提示或我应该使用的东西。

【问题讨论】:

不确定我是否理解您的问题。但我认为只是简单地将另一个带有橙色边框颜色的 UIView 放在滚动视图的顶部,就完成了 【参考方案1】:

据我了解,您希望橙色矩形始终保持在同一位置。滚动时,您可以让它消失并在滚动停止后重新出现,但这是一个细节。

因此,您可以放置​​一个与单元格尺寸相同的UIView(因为所有单元格似乎都具有相同的高度和宽度),并在用户停止滚动后显示它。

无论如何,我正在再次查看您的问题,我认为我没有正确理解您的问题,特别是这部分 The current cell (The cell which has scrolled to), gets highlighted in Orange. 与此 When the user scrolls to another cell, I want the Orange square, to remain in the first cell 结合使用

【讨论】:

如果您觉得这个问题对您有帮助,请接受,否则请告诉我们我们还能如何帮助您【参考方案2】:

过程可能如下:

将collectionView 的大小设置为单个单元格大小。并将其向左对齐 将您的 collectionView 的 clipsToBounds 属性设置为 false 启用 UICollectionView 分页。 在您的 collectionView 顶部放置一个矩形视图

【讨论】:

以上是关于SWIFT:如何修复单元格荧光笔并使整个集合视图滚动而不是荧光笔?的主要内容,如果未能解决你的问题,请参考以下文章

启用分页时将水平可滚动集合视图的单元格居中并使其他单元格部分可见

swift - 快速按下集合视图单元格时如何修复自定义视图中的约束

如何使用 Swift 在集合视图单元格中添加表视图?

如何在swift ios中调整集合视图单元格的高度?

Swift 2.1 - 如何将集合视图单元格的索引行传递给另一个视图控制器

如何在swift 3中的表视图单元格中实现集合视图的分页?