swift - UICollectionView 水平滚动与静态焦点项目

标签 swift uicollectionview uikit tvos apple-tv

我正在尝试创建一个嵌入在 tvOS 上的 UITableViewCell 中的可水平滚动的 UICollectionView,并且已经实现了我的目标。

接下来我需要做的是能够将焦点转移到 UICollectionView 中的下一个项目,但是整个项目堆栈应该向左或向右移动一个位置,但焦点将保留在视觉上的“第一个”项目。

这是我希望实现的一个 ASCII 图表:

Scenario 1, before focus shifts
     ---------------------
1.   | v   v   v   v   v |
2.   |[v]  v   v   v   v |
3.   | v   v   v   v   v |
     ---------------------

Scenario 1, after focus shifts
     ---------------------
1.   | v   v   v   v   v |
2.   | v  [v]  v   v   v |
3.   | v   v   v   v   v |
     ---------------------

上面的场景 1 是默认行为,其中焦点转移到 UICollectionView 中的下一个项目。

Scenario 2, before focus shifts
     ---------------------
1.   | v   v   v   v   v |
2.   |[v]  v   v   v   v |
3.   | v   v   v   v   v |
     ---------------------

Scenario 2, after focus shifts
     ---------------------
1.   | v   v   v   v   v |
2. v |[v]  v   v   v     |
3.   | v   v   v   v   v |
     ---------------------

场景 2 中,当我在 Remote 上滑动以选择 UICollectionView 中的下一个项目后,焦点应保持在左侧,但焦点下方的项目应移动一个。

有人对如何实现这一目标有任何提示吗?

我读过有关 UICollectionView 中粘性列的帖子,但我认为这并不完全是粘性列?

最佳答案

我最近不得不为一个项目这样做。这是我使用的:

 override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    if let focusView = context.nextFocusedView as? UICollectionViewCell, let indexPath = collection.indexPath(for: focusView) {

        collection.isScrollEnabled = false
        coordinator.addCoordinatedAnimations({
            self.collection.scrollToItem(at: indexPath, at: .left, animated: true)
        }, completion: nil)
    }
}

关于swift - UICollectionView 水平滚动与静态焦点项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51075017/

相关文章:

Swift:if let 语句中的条件类型转换(as?,as!)

swift - 数据错误的 super 初始化

Swift:为什么每次我向下和向上滚动时我的按钮都会重复?

iphone - UINavigationbar 的阴影效果,如 GameCenter

ios - CAGradientLayer 自动旋转

ios - 尽管设置了安全区域指南,但使用 XIB 的显示中的白色区域仍然显示空白

arrays - 在 Swift 数组中查找唯一值

ios - 在 iOS map 上有条件地给 MKCircle 上色

ios - 在 UITextView 上点击时不调用 UICollectionView didSelectItemAtIndexPath

ios - 无障碍订单IOS