我正在尝试创建一个嵌入在 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/