【发布时间】: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 放在滚动视图的顶部,就完成了
标签: swift uicollectionview uiscrollview tvos